Reputation: 45
i have the following code, please let me know how to create the instance at runtime without specifying the type while coding.
MyObject productobject = something.
i need the object of type
AccessValuesFrom<long,double>,AccessValuesFrom<long,string>..etc
Generally i can create the object is below:
AccessValuesFrom<long,double> accessData=productobject.ElementInfo.GetAccessValues<double>(something, something);
but i need the AccessValuesFrom at run time. long is pre-defined. how to create the instances for such type when there is no direct way of creating the instance(my situation i have to call
productobject.ElementInfo.GetAccessValues<double>(something, something)
to create the instace.
Upvotes: 1
Views: 72
Reputation: 2195
If you have a Type
object at this point, you can implement GetAccessValues
like this:
object GetAccessValues(Type type, something, something)
{
var result = Activator.CreateInstance(
typeof(AccessValuesFrom<,>).MakeGenericType(typeof(long), type));
//do something with something
return result;
}
also it'll be good to create an interface for all generic AccessValuesFrom
with common methods in it.
Upvotes: 3