1
下面的代码不会在Windows 10通用的应用程序编译,但确实在.NET控制台应用程序(包括使用Reflection):在Windows 10的通用应用程序使用的CreateInstance
string objType = "MyObjType";
var a = Assembly.GetExecutingAssembly();
var newObj = a.CreateInstance(objType);
这样看来,通用Windows应用程序不包含方法Assembly.GetExecutingAssembly()
; Assembly对象似乎也不包含CreateInstance
。
Activator.CreateInstance在.NET中有16个重载,在Win 10应用程序中只有3个。我正在引用桌面扩展。
这种类型的构造在Windows 10中仍然可能,如果是这样的话,怎么样?我想要做的是从代表该类的字符串中创建一个类的实例。
听起来像你想要的类型,例如用'Assembly.GetType(...)',然后调用'Activator.CreateInstance(Type)'。输入哪个组件? –
该类型在当前程序集中(为什么我要调用GetExecutingAssembly),因此如果能够执行程序集,我当然可以这么做。 –
那么你不能使用'typeof(Foo).Assembly',其中'Foo'是你写代码的类型吗? –