我正在尝试从Unity.config文件加载Unity配置。在接口的实现中使用了泛型参数 我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="TestUnityGeneric" />
<namespace name="TestUnityGeneric"/>
<container name="Entity">
<register type="IEventBus`2[IEvent`1[long],long]" mapTo="TestEventBus`2[IEvent`1[long],long]"></register>
<register type="ISender" mapTo="TestSender">
<constructor>
<param name="eventBus" dependencyType="IEventBus`2[IEvent`1[long], long]" />
</constructor>
</register>
</container>
</unity>
这是配置不起作用。 但下一个C#代码有效:
public interface IEvent<out Key>
{
Key SourceId { get; }
}
public interface IEventBus<E, in Key>
where E : IEvent<Key>
{
void Publish(E @event);
}
public class TestEventBus<E, Key> : IEventBus<E, Key>
where E : IEvent<Key>
{
public void Publish(E @event)
{
Console.WriteLine("Message published!");
}
}
public interface ISender
{
void SendToBus(string message);
}
public class TestSender : ISender
{
private IEventBus<IEvent<long>, long> _bus { get; set; }
public TestSender(IEventBus<IEvent<long>,long> eventBus)
{
Console.WriteLine("Sender Initialized");
_bus = eventBus;
}
public void SendToBus(string message)
{
IEvent<long> env = new TestEvent
{
SourceId = 1,
Message = message
};
_bus.Publish(env);
}
}
public class TestEvent : IEvent<long>
{
public long SourceId { get; set; }
public string Message { get; set; }
}
class Program
{
static void Main(string[] args)
{
// It Is not worked: Exception: "GenericArguments violates the constraint of type paremeter "E""
IUnityContainer container = new UnityContainer().LoadConfiguration(ConfigurationManager.AppSettings["UnityContainer"]);
// This is worked
/*
IUnityContainer container = new UnityContainer();
IEventBus<IEvent<long>, long> bus = new TestEventBus<IEvent<long>, long>();
container.RegisterInstance<IEventBus<IEvent<long>, long>>(bus);
container.RegisterType<ISender, TestSender>(new TransientLifetimeManager(),
new InjectionConstructor(typeof(IEventBus<IEvent<long>, long>)));
*/
var sender = container.Resolve<ISender>();
sender.SendToBus("MyMessage");
Console.ReadKey();
}
}
所有项目代码:HERE
答案 0 :(得分:1)
事实证明,Unity不了解Unity.config文件中的嵌套泛型。解决方案:使用Alias,Alias One不应使用其他别名。这个配置 - 工作:
<div class="container">
<div>
1
</div>
<div>
2
</div>
<div>
3
</div>
</div>
<div class="container">
<div>
3
</div>
</div>