How to get the subtype of a generic class in C# -


i have generic method

public async task save<t>(t aggregate) t : aggregateroot 

from method call generic method

var indexregistrations = indexregistrar.getall<t>(); 

now in second generic method, want real type of t, subtype of aggregateroot:

public static list<indexregistration> getall<t>() t : aggregateroot {     return _register.findall(r => r.aggregatetype == typeof(t)); } 

however, typeof(t) returns aggregateroot.

how can real type (=subtype of aggregateroot) of t?

typeof(t) correct here.

i tested case, , typeof(t) returns "true" class, not type requirement.

public class baseclass { } public class derivedclass: baseclass { }  public class genericclass<t> t : baseclass {     public string typeof = typeof(t).tostring(); }  public class genericsuperclass<t> t : baseclass {     public genericclass<t> sub = new genericclass<t>(); }  static void main(string[] args) {     console.writeline("1 - " + (new genericclass<baseclass>()).typeof);     console.writeline("2 - " + (new genericclass<derivedclass>()).typeof);      console.writeline("3 - " + (new genericsuperclass<baseclass>()).sub.typeof);     console.writeline("4 - " + (new genericsuperclass<derivedclass>()).sub.typeof);      console.readline(); } 

the output:

1 - baseclass 2 - derivedclass     3 - baseclass 4 - derivedclass 

note i've simplified classnames values returned (e.g sandbox.testconsole.program+derivedclass).

this directly contradicts claim ever base type (aggregateroot, in case).


reflection exception this.

i can think of 1 exception this: when type defined @ runtime (e.g. generated type name (string)).
however, this stackoverflow answer explains, generics intended provide compile time type safety.

it's not impossible use reflection instantiate generic class @ runtime. when so, inherently preventing validity of information (e.g. type names) decided @ compile-time.
msdn's page on typeof implicitly states return value of typeof compile-time type.

combining these 2 facts, means when use reflection (i.e. deciding type @ runtime), cannot rely on typeof (as returns compile time type).


the linked msdn page mentions how find runtime type:

to obtain run-time type of expression, can use .net framework method gettype, in following example:

int = 0; system.type type = i.gettype(); 

however, note gettype() method available on instantiated object, not on generic type parameter. generic type parameters not types, closer "type placeholders".

you need either pass type parameter, or instantiated object (of appropriate type).


conclusion:

if using types known @ compile time, can use typeof(t), example has shown.

if deciding type on runtime using reflection, cannot rely on information provided typeof(t), , therefore required supply type. either supply type parameter, or supply via instantiated object, runtime type can accurately tested using gettype() method.

however, if deciding type on runtime, better off passing type itself parameter. if you're using reflection here, means must @ point have known type wanted use. therefore, pass known type parameter, can use subsequent business logic.

i cannot think of single scenario in aren't either (a) using type known @ compile time, nor (b) aware (at runtime) of type you've decided use.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -