简体   繁体   中英

Using Ninject create an instance of an interface

In our middleware, interfaces are bound in Global.asax.cs . I created a HandshakeInvoker that reflects through all FieldInfo until it finds an interface that implements IESFPingable . When it finds one i need to create an instance of that interface which will then call the ping() class in the provider. My problem is I am having trouble creating the interface once it finds a match.

Global.asax.cs binds:

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
}

I get errors:

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...)

Does anyone see what i am doing incorrectly? All help greatly appreciated.

EDIT: We use Ninject to instantiate our interfaces. I am told i should need to use kernel.Get. The reflection returns the interface as a string (f.FieldType.Name), which throws errors in Get. But besides that it doesn't matter what i put in kernel.Get. I get a type or namespace error. I've tried to do Type myType = Type.GetType(f.FiedlType.Name) and put kernel.Get and it gets the same error. I've tried a hundred different things and so have others with no luck. I've been told that the Generic answer this post referred me to does not use Ninject and will not work here.

I just need to call a methodin a provider class in another assembly. The Interface/Provider is included in the bindings in the Global.asax but no matter what we try it doesn't work. I am very frustrated. Do I need to use DependencyResolver? Is this not even possible? It has to be possible.

This works much better. I didn't need to create a new instance of the interface.

    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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM