2009-09-22 88 views
2

我需要確定重載的構造通過使用反射.The部分代碼是值:C# - 反射 - 識別重載的構造

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

假設類型MedicalInsuranceRules包含重載構造

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

如何在反射過程中匹配excat constructor?

回答

4

Type.GetConstructor有一個重載,您可以指定構造函數參數的類型,例如

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

使用GetConstructors方法,而不是GetConstructor,再看看每個獲得的ConstructorInfoParameters屬性,以搜索想要的人。