I have an open generic interface, which i implement with a non generic class, and i want to inject this class using castle windsor, but am struggling…..
Lets say i have the following interface
public interface IMyInterface<T>
{
bool DoSomething(T param);
}
And then i have the following class that implements this interface like so
public class MyClass : IMyInterface<string>
{
public bool DoSomething(string param)
{
// Do Something
return true;
}
}
I want to be able to resolve the interface using castle like this, so that the injectedObject becomes an instance of MyClass.
WindsorContainer container = new WindsorContainer(new XmlInterpreter(newConfigResource("castle")));
IMyInterface<string> injectedObject = container.Resolve<IMyInterface<string>>();
Is this possible, or have i gone off track slightly? If its possible, how do i set up the castle config section? I have tried using the ‘1 notation to indicate that the interface is an open generic type, but you get an error if the implementation is not also generic, which in this case it is not.
Any help appreciated
We use the fluent interface in my team so it’s been a while since I’ve looked at the config file syntax. The basic principle is this: your service is
IMyInterface<string>and the implementing type isMyClass. So I think it would be something like this:You say you get an error. What is the error? I guess it is because you have defined the service as
IMyInterface<>without supplying the type parameter. If you want to do that, as your question implies, the implementing type must also be generic:Note that you can register both components. If you do that, resolving
IMyInterface<string>will get you an instance ofMyClass, while resolvingIMyInterface<AnyOtherType>will get you an instance ofMyGenericClass<AnyOtherType>.