mercredi 5 avril 2017

How to get the ConstructorInfo of a Ctor with params

I am trying to write code that receives an argument list and gets the ConstructorInfo of a matching Ctor.

The method signature is ConstructorInfo GetConstructorInfo(Type type, object[] args).

I have created a class to work with:

public class ClassWithParamsInCtor
{
    public ClassWithParamsInCtor(params int[] parameters)
    {

    }
}

Using the Activator class I can create instances of this object:

ClassWithParamsInCtor myclass = Activator.CreateInstance(typeof(ClassWithParamsInCtor), new object[] { 1,2 }) as ClassWithParamsInCtor; \\returns a valid instance of the class;

But when I try to get the ConstructorInfo there is an issue, the following returns null:

ConstructorInfo ctorInfo = typeof(ClassWithParamsInCtor).GetConstructor(new Type[] { typeof(int), typeof(int) }); \\returns null

How can I obtain the ConstructorInfo in such a case ?





Aucun commentaire:

Enregistrer un commentaire