instantiating a generic referance class with data types known at runtime

124 views Asked by At

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

1

There are 1 answers

1
cyberj0g On

Does ResolvedChainSubscriber have any public constructors? Try to use Activator.CreateInstance overload that takes parameters for existing public constructor.