Compact Framework - how do I dynamically create type with no default constructor?

前端 未结 3 1816
悲哀的现实
悲哀的现实 2020-12-20 21:20

I\'m using the .NET CF 3.5. The type I want to create does not have a default constructor so I want to pass a string to an overloaded constructor. How do I do this?

3条回答
  •  失恋的感觉
    2020-12-20 21:39

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

提交回复
热议问题