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)
{
    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

Spider man
  • 3,224
  • 4
  • 30
  • 42
  • are u asking about dynamic datatype? – user786 Jun 10 '15 at 04:31
  • well, i'm saying that the type of the second data type, that is "V", will only be known at run time.... and it is stored in ProtoType – Aditi Srivastava Jun 10 '15 at 04:36
  • Issue here doesn't seems to be related to type resolution, which should happen at compile time for the generics, else you need reflection to resolve the type, check the links I have posted underneath, if they help – Mrinal Kamboj Jun 10 '15 at 04:50

1 Answers1

0

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

cyberj0g
  • 3,707
  • 1
  • 19
  • 34
  • Do, i still need a constructor is i want to assign the value of another Object class object to obj1. for example if i have (Object object) in the parameters for subscriberTopic...... and i want to typecast this object and store it in obj1 – Aditi Srivastava Jun 10 '15 at 05:35