简体   繁体   中英

C# - Reflection -Identifying Overloaded constructor

I need to identify the overloaded constructor to pass the values using reflection .The Partial code is :

            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 type MedicalInsuranceRules 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 contructor during reflection ?

Type.GetConstructor有一个重载,您可以在其中指定构造函数参数的类型,例如

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

使用将类型数组作为参数的GetConstructor重载

使用GetConstructors方法而不是GetConstructor ,然后查看获得的每个ConstructorInfoParameters属性,以搜索所需的属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM