使用 Ninject 创建接口实例
Using Ninject create an instance of an interface
在我们的中间件中,接口绑定在Global.asax.cs
中。我创建了一个 HandshakeInvoker
,它反映了所有 FieldInfo
,直到它找到一个实现 IESFPingable
的接口。当它找到一个时,我需要创建该接口的一个实例,然后调用提供程序中的 ping() class。我的问题是一旦找到匹配我就无法创建界面。
Global.asax.cs
绑定:
kernel.Bind<IAccountProvider>().To<AccountProvider>().InSingletonScope();
HandshakeInvoker
:
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
outputs = new object[0];
Type interfaceType = typeof(IESFPingable);
FieldInfo[] fields = serviceType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo f in fields)
{
if (interfaceType.IsAssignableFrom(f.FieldType))
{
Console.WriteLine(" >> Field {0} Implements {1}", f.fieldType.Name, interfaceType.Name);
var interfaceToPing = kernel.Get<f.FieldType.Name>();
}
}
return DateTime.Now; //return date for now
}
我收到错误:
The Name 'kernel' does not exist in the current context and The type
or namespace name 'f' could not be found (are you missing a using
directive...)
有没有人看到我做错了什么?非常感谢所有帮助。
编辑:我们使用 Ninject 来实例化我们的接口。有人告诉我我应该需要使用 kernel.Get。反射 returns 接口作为字符串 (f.FieldType.Name),这会在 Get 中抛出错误。但除此之外,我输入 kernel.Get 的内容并不重要。我收到类型或命名空间错误。我试过
键入 myType = Type.GetType(f.FiedlType.Name) 并输入 kernel.Get 并得到相同的错误。我已经尝试了一百种不同的东西,其他人也没有运气。有人告诉我,这个 post 向我推荐的通用答案不使用 Ninject 并且不会在这里工作。
我只需要在另一个程序集中的提供程序 class 中调用一个方法。 Interface/Provider 包含在 Global.asax 的绑定中,但无论我们如何尝试,它都不起作用。我很沮丧。我需要使用 DependencyResolver 吗?这甚至不可能吗?它必须是可能的。
这样效果更好。我不需要创建接口的新实例。
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
outputs = new object[0];
Type interfaceType = typeof(IESFPingable);
Type T = instance.GetType();
FieldInfo[] fields = T.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var f in fields)
{
var result = f.GetValue(instance);
if (result is IESFPingable)
{
(result as IESFPingable).Ping();
}
}
return DateTime.Now; //temporary
}
在我们的中间件中,接口绑定在Global.asax.cs
中。我创建了一个 HandshakeInvoker
,它反映了所有 FieldInfo
,直到它找到一个实现 IESFPingable
的接口。当它找到一个时,我需要创建该接口的一个实例,然后调用提供程序中的 ping() class。我的问题是一旦找到匹配我就无法创建界面。
Global.asax.cs
绑定:
kernel.Bind<IAccountProvider>().To<AccountProvider>().InSingletonScope();
HandshakeInvoker
:
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
outputs = new object[0];
Type interfaceType = typeof(IESFPingable);
FieldInfo[] fields = serviceType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo f in fields)
{
if (interfaceType.IsAssignableFrom(f.FieldType))
{
Console.WriteLine(" >> Field {0} Implements {1}", f.fieldType.Name, interfaceType.Name);
var interfaceToPing = kernel.Get<f.FieldType.Name>();
}
}
return DateTime.Now; //return date for now
}
我收到错误:
The Name 'kernel' does not exist in the current context and The type or namespace name 'f' could not be found (are you missing a using directive...)
有没有人看到我做错了什么?非常感谢所有帮助。
编辑:我们使用 Ninject 来实例化我们的接口。有人告诉我我应该需要使用 kernel.Get。反射 returns 接口作为字符串 (f.FieldType.Name),这会在 Get 中抛出错误。但除此之外,我输入 kernel.Get 的内容并不重要。我收到类型或命名空间错误。我试过 键入 myType = Type.GetType(f.FiedlType.Name) 并输入 kernel.Get 并得到相同的错误。我已经尝试了一百种不同的东西,其他人也没有运气。有人告诉我,这个 post 向我推荐的通用答案不使用 Ninject 并且不会在这里工作。
我只需要在另一个程序集中的提供程序 class 中调用一个方法。 Interface/Provider 包含在 Global.asax 的绑定中,但无论我们如何尝试,它都不起作用。我很沮丧。我需要使用 DependencyResolver 吗?这甚至不可能吗?它必须是可能的。
这样效果更好。我不需要创建接口的新实例。
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
outputs = new object[0];
Type interfaceType = typeof(IESFPingable);
Type T = instance.GetType();
FieldInfo[] fields = T.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var f in fields)
{
var result = f.GetValue(instance);
if (result is IESFPingable)
{
(result as IESFPingable).Ping();
}
}
return DateTime.Now; //temporary
}