【发布时间】:2014-08-07 01:37:09
【问题描述】:
我正在尝试使用以下代码创建一个类的实例(在运行时通过字符串):
Assembly assembly = Assembly.GetAssembly(typeAssembly);
object instance = assembly.CreateInstance(typeName); //throws MissingMethodException
Type classType = instance.GetType();
但是,我尝试实例化的类在构造函数中有两个参数,由 Unity IoC 容器解析。
当我执行上述代码时,它会抛出“System.MissingMethodException”。
我在网上搜索过,看来 Assembly.CreateInstance 将无法解决 Unity 依赖注入。
这是一个死胡同还是有办法使用“CreateInstance”方法实例化类并解决 Unity 依赖项?
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# reflection unity-container ioc-container