c# - Compact Framework - 如何动态创建没有默认构造函数的类型?

标签 c# reflection compact-framework

我使用的是.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/

相关文章:

c# - 作为数组的 XmlSerialization 集合

c# - ASP.NET 表单字段不从 colorbox 发布

c# - 如何做最小起订量可见的内部接口(interface)?

java - 使用反射从 XML 文件(Java 中)读取数据

c# - 与 wcf REST 通信的 JSON.NET 兼容序列化

c# - 如何在 C# (.NET Compact Framework) 中隐藏沙漏图标

c# - PLINQ、任务、异步、生产者/消费者队列?用什么?

java - Java中如何通过反射获取方法参数的值?

c++是否有类似typeof(Foo)的东西

windows-mobile - 在 ad hoc wifi windows 移动设备之间进行通信的更好方法