Activator.CreateInstance 和 Ninject 在 asp.net mvc 4 上
Activator.CreateInstance and Ninject on asp.net mvc 4
我正在尝试在同一个项目中使用反射和 ninject。这是我的代码:
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, null);
这是我的 class,其中包含该方法:
public class DataClass
{
private IEventService eventService;
public DataClass(IEventService eventService)
{
this.eventService = eventService;
}
public String Events_ListAll()
{
List<Event> lstEvents = eventService.GetEvents().ToList<Event>();
return "";
}
}
我收到一条错误消息,提示未找到构造函数。解决方案是添加一个空的默认构造函数,但这不会注入我想要的 class。有解决办法吗?
你需要一个 IEventService
的具体实例作为参数传递给 DataClass
的 ctor,就像这样 Activator.CreateInstance(type, instance);
,所以你有很多方法可以做到这一点,请参阅 2 of :
1st - class 有一个具体的 IEventService
That class where you doing the reflection has a concrete instance of IEventService
and then you just pass as param to the Activator:
public class Foo
{
public Foo(IEventService eventService)
{
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, eventService);
}
}
2nd - 获取 Ninject
的 IKernel 实现
If you are using NinjectWebCommom
you can just change the bootstrapper prop to public and get the kernel like this NinjectWebCommom.bootstrapper.Kernel.get<IEventService>()
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, Kernel.Get<IEventService>());
我正在尝试在同一个项目中使用反射和 ninject。这是我的代码:
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, null);
这是我的 class,其中包含该方法:
public class DataClass
{
private IEventService eventService;
public DataClass(IEventService eventService)
{
this.eventService = eventService;
}
public String Events_ListAll()
{
List<Event> lstEvents = eventService.GetEvents().ToList<Event>();
return "";
}
}
我收到一条错误消息,提示未找到构造函数。解决方案是添加一个空的默认构造函数,但这不会注入我想要的 class。有解决办法吗?
你需要一个 IEventService
的具体实例作为参数传递给 DataClass
的 ctor,就像这样 Activator.CreateInstance(type, instance);
,所以你有很多方法可以做到这一点,请参阅 2 of :
1st - class 有一个具体的 IEventService
That class where you doing the reflection has a concrete instance of
IEventService
and then you just pass as param to the Activator:
public class Foo
{
public Foo(IEventService eventService)
{
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, eventService);
}
}
2nd - 获取 Ninject
的 IKernel 实现If you are using
NinjectWebCommom
you can just change the bootstrapper prop to public and get the kernel like thisNinjectWebCommom.bootstrapper.Kernel.get<IEventService>()
Type type = Type.GetType("MySolution.Project.Web.App_Code.DataClass");
MethodInfo theMethod = type.GetMethod("Events_ListAll");
object classInstance = Activator.CreateInstance(type, Kernel.Get<IEventService>());