I have created a generic class, but I know the type in runtime, not in design, so I would like to know how to set the type in runtime.
For example, I have:
public class MyGenericClass<T>
{
....
}
Then I try to use it. I have a method in other class, that consume this generic class. In the constructor of this class, I receive as parameter the type that I want, so I have a type property in which I save the type that I need. So I am trying this:
MyGenericClass<_typeNeeded> myClass = new MyGenericClass<typeNeeded>();
But this does not work.
How can I set the type in runtime in a class that I created?
I am using C# 4.0.
Thanks. Daimroc.
EDIT: What I want to do is the following. I have a class that need to do some queries to the database. This queries always return the same information, a class, but the information that contains this class come from different tables. This is because I need to determinate what query to use. To decide what query to use I use the type that I receive.
Is for this reason that I don't know in design the type, but is in runtime.
I could use an interface that it would be implemented by to classes, and use the interface instantiated with the correct class, but this make me to have a switch or an if in the moment of the instantiation, and this is what I try to avoid, I want something more generic. Also, If I use this solution, to have an if in the moment of the instantion, I can create the generic class, so I would have only one class and it would be more maintainable.
I don't have enough information about your code, but maybe you could use simple polymorphism:
You could also mix this with factory design pattern, so that some other class will be responsible for instantiation of your types based on some runtime knowledge.
This of course assume that your MyGenericClass dosen't have methods like:
or if it does there is an additional relationship between all T'types so that all of them have common superclass lets say BaseT and you can change
to:
You can create your class in another way, passing to the constructor the type that you want to use and exploit the
dynamic
keyword.For example:
Have a look at the
MakeGenericType
method. You can use it to create aType
instance (which can then be used to create an instance of that type) with generic argument values determined at runtime. Be aware, though, that you will need more reflection whenever dealing with the generic features of that object; you cannot create a variable in code whose type is a generic type with a variable type argument.If you think about it, there is no reason to have a generic whose type is determined at runtime. Generics provide strong-typing, but this is only possible if you know the type at compile time. This is one of the strongest benefits of generics that it provides safety because you know what actions the object can perform. At runtime if you do not know what type you are getting, that defeats this purpose and you will have to delve into the powerful (but sometimes dangerous) world of reflection. For example,
Because I know the type at COMPILE time, I can store it in a generic container and I have type safety when using it. I know the item has a Minute and Hour property.
If you want to perform an action based on what type the object is then you use the GetType() method. This is your window into runtime type identification and processing.
If you are going to do something with the object then you either know the Method name at compile-time which means you can use interfaces,
If you do not have or want to use an interface you can use reflection or dynamics, in this case I would use dynamic if I know what method I want to call.
Lastly if you do not know the method you want to call at compile time you can use reflection.