0

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, Object obj)
{
    Type[] type={Type.GetType("System.String"),ProtoType};
    Type resolve = typeof(ResolvedChainSubscriber <,>).MakeGenericType(type); 
    Object obj1=Activator.CreateInstance(resolve);
     //now i want to typecast the parameter obj and store it in the new variable obj1
    obj1=(some statement here)obj;


}
  • 1
    Re: "This doesn't seem to be working", please be more specific. *How* does it not work? What result does this produce, and what did you expect instead? – stakx - no longer contributing Jun 09 '15 at 07:34
  • Re: `Type.GetType("System.String")`, you can simplify this to `typeof(string)`. – stakx - no longer contributing Jun 09 '15 at 07:35
  • `ResolvedChainSubscriber` is silly - the generic type arguments have to be known at compile-time, and they have to be types. You already have the type you want in `resolve`, so just do `Activator.CreateInstance(resolve)` - that gives you an instance of the given type. It sounds like you have some big misunderstandings of how generics work - what are you actually trying to do? – Luaan Jun 09 '15 at 07:37
  • So, i used Object obj1 = Activator.CreateInstance(resolve); But i get the following exception: MissingMethodException : No parameterless constructor defined for this object. – Aditi Srivastava Jun 09 '15 at 07:45

0 Answers0