我有以下代碼:StructureMap - 註冊泛型類型
public class Dispatcher : IDispatcher {
public void Send(Order order) {
var type = typeof(IOrderHandler<>).MakeGenericType(order.GetType());
var handler = ObjectFactory.GetInstance(type);
}
}
注:我試圖讓IOrderHandler其中爲了在這種情況下是: 公共類CoffeOrder:訂單{}
我的StructureMap配置如下:
ObjectFactory.Initialize(x => {
x.For<IOrderHandler>().Use<OrderHandler>();
});
當我調試它時,我得到以下錯誤或: {「StructureMap配置失敗:\ r \ n錯誤:104 \ r \ n資源:註冊表:StructureMap.Configuration.DSL.Registry,StructureMap,版本= 2.6.4.0,...
我嘗試了其他選項,我總是以104錯誤或202錯誤結束: 「StructureMap異常代碼:202 \ n沒有默認實例爲PluginFamily Proj.Test.Tests + IOrderHandler`1 [[Proj.Test.Tests + CoffeOrder,Proj.Test, ...
注意:CoffeOrder是我在測試中傳遞的訂單的類型。
我爲Handler類和接口的代碼如下:
public class CoffeHandler : OrderHandler<CoffeOrder> {
public override void Handle(CoffeOrder order) {
// Some Code
} // CoffeHandler
}
public interface IOrderHandler {
void Handle(Order order);
} // IOrderHandler
public interface IOrderHandler<TOrder> : IOrderHandler where TOrder : Order {
void Handle(TOrder order);
} // IOrderHandler
public abstract class OrderHandler : IOrderHandler {
public abstract void Handle(Order order);
} // OrderHandler
public abstract class OrderHandler<TOrder> : OrderHandler, IOrderHandler<TOrder> where TOrder : Order {
public override void Handle(Order order) {
TOrder torder = (TOrder)order;
Handle(torder);
} // Handle
public abstract void Handle(TOrder order);
} // OrderHandler
可能有人,請幫幫我嗎?
謝謝你, 米格爾