vendredi 20 mai 2016

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().To().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 = kernal.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.





Aucun commentaire:

Enregistrer un commentaire