C # - Reflection - Overloaded Constructor Identification

I need to identify an overloaded constructor to pass values ​​using reflection. Partial code:

            asm=Assembly.Load("RulesLibrary");

            Type t = asm.GetType("RulesLibrary.MedicalInsuranceRules");

            object ActObj = Activator.CreateInstance(t); 

            object[] conparam = new object[2];

            conparam[0] = "RuleID"; // string

            conparam[1] =12; // int

// How to find out the overloaded constructor in Type MedicalInsuranceRules

            ConstructorInfo cinfo = t.GetConstructor();

            cinfo.Invoke(ActObj, conparam);

      

Suppose the MedicalInsuranceRules type contains overloaded constructors

 public   MedicalInsuranceRules( ){}
 public   MedicalInsuranceRules(string ruleID,int subSection ){}
 public   MedicalInsuranceRules(string ruleID,
                                int subSection,string majorDocID ){}

      

How can I match the excat constructor during reflection?

+2


source to share


3 answers


Type.GetConstructor has an overload where you can specify the types of the constructor arguments, eg.



var ciInfo = t.GetConstructor(new[] { typeof(string), typeof(int) });

      

+4


source


Use GetConstructor overload , which takes an array of types as a parameter.



+1


source


Use the method GetConstructors

instead GetConstructor

, then look at the property of Parameters

each ConstructorInfo

obtained to find the ones you want.

0


source







All Articles