Castle windsor - 汽车不满足实现类型的通用约束
Castle windsor - Car don't satisfy generic constraints of implementation type
我正在使用 castle windsor 进行依赖注入并尝试注入一些通用类型。但是,当我在我的容器上调用 resolve 时,出现以下错误:
Class doesn't satisfy generic constraints of implementation type.
在这个例子中,我正在传递一个简单的 Car
类型。
public class CreateRequestInstance<T> where T : class, ICreateRequestInstance<T>
{
private IRequestObject<T> _IRequestObject;
public CreateRequestInstance(IRequestObject<T> requestObject)
{
this._IRequestObject = requestObject;
}
public TReturn CreateRequest<TReturn>(string provider, string type, string token, T request,
string revision, string errorCode, string errorMessage)
{
_IRequestObject.Provider = provider;
_IRequestObject.Type = type;
_IRequestObject.Token = token;
_IRequestObject.Request = request;
_IRequestObject.ErrorCode = errorCode;
_IRequestObject.ErrorMessage = errorMessage;
return (TReturn)Convert.ChangeType(_IRequestObject, typeof(IRequestObject<T>));
}
}
这是我的安装程序:
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container
.Register(Component
.For(typeof(ICreateRequestInstance<>))
.ImplementedBy(typeof(CreateRequestInstance<>))
.LifestyleTransient())
.Register(Component
.For(typeof(ICreateResponseInstance<>))
.ImplementedBy(typeof(CreateResponseInstance<>))
.LifestyleTransient())
.Register(Component
.For(typeof(IRequestObject<>))
.ImplementedBy(typeof(RequestObject<>))
.LifestyleTransient())
.Register(Component
.For(typeof(IResponseObject<>))
.ImplementedBy(typeof(ResponseObject<>))
.LifestyleTransient());
}
这是我的 class 工厂调用:
public T GetConcreteClass<T>()
{
var someClass = container.Resolve<T>();
return someClass;
}
这是调用代码:
var t = factory.GetConcreteClass<ICreateRequestInstance<ICar>>();
var test = t.CreateRequest<ICar>("provide", "test", "test", car, "1", "this is a test", "fred");
如有任何帮助,我们将不胜感激。
public class CreateRequestInstance<T> where T : class, ICreateRequestInstance<T>
这看起来不对。您声明的 class 需要一个模板参数,该模板参数是实现 ICreateRequestInstance<T>
的 class。有了这个,你会得到你看到的错误,除非你的 Car
类型实现 ICreateRequestInstance<T>
(我怀疑它确实如此)。
我认为你真的有意:
public class CreateRequestInstance<T> : ICreateRequestInstance<T> where T : class
这是一个实现 ICreateRequestInstance<T>
的 class,它需要一个模板参数,它是任何 class。
我正在使用 castle windsor 进行依赖注入并尝试注入一些通用类型。但是,当我在我的容器上调用 resolve 时,出现以下错误:
Class doesn't satisfy generic constraints of implementation type.
在这个例子中,我正在传递一个简单的 Car
类型。
public class CreateRequestInstance<T> where T : class, ICreateRequestInstance<T>
{
private IRequestObject<T> _IRequestObject;
public CreateRequestInstance(IRequestObject<T> requestObject)
{
this._IRequestObject = requestObject;
}
public TReturn CreateRequest<TReturn>(string provider, string type, string token, T request,
string revision, string errorCode, string errorMessage)
{
_IRequestObject.Provider = provider;
_IRequestObject.Type = type;
_IRequestObject.Token = token;
_IRequestObject.Request = request;
_IRequestObject.ErrorCode = errorCode;
_IRequestObject.ErrorMessage = errorMessage;
return (TReturn)Convert.ChangeType(_IRequestObject, typeof(IRequestObject<T>));
}
}
这是我的安装程序:
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container
.Register(Component
.For(typeof(ICreateRequestInstance<>))
.ImplementedBy(typeof(CreateRequestInstance<>))
.LifestyleTransient())
.Register(Component
.For(typeof(ICreateResponseInstance<>))
.ImplementedBy(typeof(CreateResponseInstance<>))
.LifestyleTransient())
.Register(Component
.For(typeof(IRequestObject<>))
.ImplementedBy(typeof(RequestObject<>))
.LifestyleTransient())
.Register(Component
.For(typeof(IResponseObject<>))
.ImplementedBy(typeof(ResponseObject<>))
.LifestyleTransient());
}
这是我的 class 工厂调用:
public T GetConcreteClass<T>()
{
var someClass = container.Resolve<T>();
return someClass;
}
这是调用代码:
var t = factory.GetConcreteClass<ICreateRequestInstance<ICar>>();
var test = t.CreateRequest<ICar>("provide", "test", "test", car, "1", "this is a test", "fred");
如有任何帮助,我们将不胜感激。
public class CreateRequestInstance<T> where T : class, ICreateRequestInstance<T>
这看起来不对。您声明的 class 需要一个模板参数,该模板参数是实现 ICreateRequestInstance<T>
的 class。有了这个,你会得到你看到的错误,除非你的 Car
类型实现 ICreateRequestInstance<T>
(我怀疑它确实如此)。
我认为你真的有意:
public class CreateRequestInstance<T> : ICreateRequestInstance<T> where T : class
这是一个实现 ICreateRequestInstance<T>
的 class,它需要一个模板参数,它是任何 class。