passing type of property to generic type

Abraham Josef

I have a generic class GenericClass<T> for some reason I need to pass the generic type from another type like that:

say I have some classes from NormalClass1 to NormalClassN all of them have property say prop1 with different types I need to do this

var type1 = typeof(NormalClass1).GetProperty("prop1").GetType();

and send type1 to new instance of GenericClass like that:

 var instance = new GenericClass<type1>();

but an error has occurred that says that

Cannot implicitly convert type 'GenericClass<type1>' to 'GenericClass<T>'   

how can I pass this type to GenericClass

crazy_crank

There are multiple problems with your code. First of all:

var type1 = typeof(NormalClass1).GetProperty("prop1").GetType();

will return the type PropertyInfo, not the type of the property. What you want is:

var type1 = typeof(NormalClass1).GetProperty("prop1").PropertyType;

Second of all you seem to have a conceptual issue with Generics, Types and TypeParameters.

Basically there is a difference between a Type variable (Type x = typeof(NormalClass1<>) and a generic Type parameter (the T in NormalClass<T>). T is not much more than a placeholder for a Type. You can use typeof(T) to get the actual Type of T. Using typeof(x) on the other hand would result in a compulation error since x is a variable and not a Type. You would use x.GetType() instead.

You cannot create a generic type via a runtime type variable directly. What you can do instead is creating the generic type via reflection.

The following example should illustrate how to do that

var genericTypeParameter = typeof(NormalClass1).GetProperty("prop1").PropertyType;
var genericBaseType = typeof(GenericClass<>);
var genericType = genericBaseType.MakeGenericType(genericTypeParameter);
var instance = Activator.CreateInstance(genericType);

As you can see, var instance would substitute to object instance. That has to be that way, since you can check the type of compile time. Best Practice would probably be to create a non generic base class for you generic class. You can than use the base class type and have at least a small amount of type checking at runtime, even if you have no chance to test the generic type parameter.

This would be what it would look like:

var instance = (GenericClassBase)Activator.CreateInstance(genericType);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing in a "TYPE" to a Generic_Interface type Property

From Dev

Issue with passing generic type

From Dev

Issue with passing generic type

From Dev

Passing generic type to a generic interface

From Dev

Passing generic type to a generic interface

From Dev

Passing property type as parameter

From Dev

generic type of class property

From Dev

Get type of property in generic method

From Dev

Getting property type and convert generic

From Dev

Get ICollection property of generic type

From Dev

Swift generic type property and method

From Dev

Generic c# property type

From Dev

Java: Passing generic type to an another generic type class

From Dev

Java: Passing generic type to an another generic type class

From Dev

Passing a generic type to a method which takes a generic type

From Dev

Passing array to function with generic data type argument

From Dev

Passing parameter to the constructor of a generic Class type

From Dev

Passing generic type over functions in Swift

From Dev

Passing a control as a generic parameter type and inheriting from it

From Dev

C# passing a compare class as a generic type

From Dev

Passing variable type to recursive factory as generic

From Dev

Passing a (local) variable to a generic method as Type

From Dev

Passing generic type from one class to another?

From Dev

Jackson Serialization of Class with Generic Property losing type information of the generic property

From Dev

Passing a class with type parameter as type parameter for generic method in Java

From Dev

Passing Type variable as Generic Type Parameter to Static Class

From Dev

Telling a generic function its type without passing an example of that type

From Dev

Avoid passing generic type parameter of class extending generic class

From Dev

Generic Method Won't Recognise Generic Type When Passing It as a Parameter

Related Related

  1. 1

    Passing in a "TYPE" to a Generic_Interface type Property

  2. 2

    Issue with passing generic type

  3. 3

    Issue with passing generic type

  4. 4

    Passing generic type to a generic interface

  5. 5

    Passing generic type to a generic interface

  6. 6

    Passing property type as parameter

  7. 7

    generic type of class property

  8. 8

    Get type of property in generic method

  9. 9

    Getting property type and convert generic

  10. 10

    Get ICollection property of generic type

  11. 11

    Swift generic type property and method

  12. 12

    Generic c# property type

  13. 13

    Java: Passing generic type to an another generic type class

  14. 14

    Java: Passing generic type to an another generic type class

  15. 15

    Passing a generic type to a method which takes a generic type

  16. 16

    Passing array to function with generic data type argument

  17. 17

    Passing parameter to the constructor of a generic Class type

  18. 18

    Passing generic type over functions in Swift

  19. 19

    Passing a control as a generic parameter type and inheriting from it

  20. 20

    C# passing a compare class as a generic type

  21. 21

    Passing variable type to recursive factory as generic

  22. 22

    Passing a (local) variable to a generic method as Type

  23. 23

    Passing generic type from one class to another?

  24. 24

    Jackson Serialization of Class with Generic Property losing type information of the generic property

  25. 25

    Passing a class with type parameter as type parameter for generic method in Java

  26. 26

    Passing Type variable as Generic Type Parameter to Static Class

  27. 27

    Telling a generic function its type without passing an example of that type

  28. 28

    Avoid passing generic type parameter of class extending generic class

  29. 29

    Generic Method Won't Recognise Generic Type When Passing It as a Parameter

HotTag

Archive