mardi 9 juin 2015

instantiating a generic referance class with data types known at runtime

I have a generic class in C# which has 2 data types public class ResolvedChainSubscriber < K, V>, now I have to create an instance of this class, but the data type of one of the arguments will only be known at runtime. I have it stored as "Type Prototype".

public static Object SubsciberTopic(Type ProtoType)
{
    Type[] type={Type.GetType("System.String"),ProtoType};
    Type resolve = typeof(ResolvedChainSubscriber <,>).MakeGenericType(type); 
    Object obj1=Activator.CreateInstance(resolve);


}

However, i get the exception MissingMethodException: NO parameterless constructor defined for this object

Please note that the class ResolvedChainSubscriber is a reference class.. hence i cannot edit it to add a function or constructor





Aucun commentaire:

Enregistrer un commentaire