3
調用構造函數像下面一個非常簡單的類,的反思與ConstructorInfo
class Program
{
public Program(int a, int b, int c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}
,我使用反射來調用構造函數
這樣的事情...
var constructorInfo = typeof(Program).GetConstructor(new[] { typeof(int), typeof(int), typeof(int) });
object[] lobject = new object[] { };
int one = 1;
int two = 2;
int three = 3;
lobject[0] = one;
lobject[1] = two;
lobject[2] = three;
if (constructorInfo != null)
{
constructorInfo.Invoke(constructorInfo, lobject.ToArray);
}
但是我收到一個錯誤,提示「對象與目標類型構造函數信息不匹配」。
任何幫助/意見非常感謝。 在此先感謝。
唷!我怎麼錯過這個?謝謝.... –
假設如果我在構造函數中有一個keyvalue對,那麼我可以用同樣的方法調用嗎? –
@nowhewhomustnotbenamed。,看我的編輯.... – horgh