我使用的是.NET CF 3.5。我想要创建的类型没有默认构造函数,因此我想将字符串传递给重载的构造函数。我该怎么做?
代码:
Assembly a = Assembly.LoadFrom("my.dll");
Type t = a.GetType("type info here");
// All ok so far, assembly loads and I can get my type
string s = "Pass me to the constructor of Type t";
MyObj o = Activator.CreateInstance(t); // throws MissMethodException
最佳答案
MyObj o = null;
Assembly a = Assembly.LoadFrom("my.dll");
Type t = a.GetType("type info here");
ConstructorInfo ctor = t.GetConstructor(new Type[] { typeof(string) });
if(ctor != null)
o = ctor.Invoke(new object[] { s });
关于c# - Compact Framework - 如何动态创建没有默认构造函数的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29436/