Want to create an instance of a class with activation custom data, like following:
var repo = Activator.CreateInstance(Type.GetType(repoFullName),
new string[] { connectionString }) as IRepository;
If I don’t pass second parameter which is string array in CreateInstance method everything works fine, but with custom data parameter method throws the exception
System.MissingMethodException: Constructor on type ‘Namespase.MockRepository’ not found.
Public constructors of instantiated type
public MockRepository(string[] customData) {...}
or
public MockRepository(Object[] customData) {...}
didn’t change the effect. What do I do wrong?
You need to revise the constructor signature, right now the way you are calling it is passing a single string/object and not an array so it isn’t able to find a matching constructor, if you want to activate an object view reflection in this manner the object must have a matching constructor to the calling code. The reason you use an object array to pass arguments to activation is to allow multiple parameters to be passed to the constructor based on position and the types must match the definition.
If you had a constructor of:
It should instantiate correctly, this being said if you also plan to instantiate without passing any parameters to the constructor you will need to define a parameterless constructor as well.