Unity不解决嵌套的通用参数使用配置文件,但解决使用UnityContainer

本文关键字:解决 配置文件 UnityContainer Unity 嵌套 参数 | 更新日期: 2023-09-27 18:18:34

我正在尝试从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>

这是配置不工作。

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

Unity不解决嵌套的通用参数使用配置文件,但解决使用UnityContainer

已解析

事实证明,Unity并不理解Unity中的嵌套泛型。配置文件。解决方法:使用"Alias","Alias"不能使用其他别名。此配置有效:

<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <assembly name="TestUnityGeneric" />
  <namespace name="TestUnityGeneric"/>
  <alias alias="IEventLong" type="TestUnityGeneric.IEvent`1[System.Int64], TestUnityGeneric" />
  <container name="Entity">
    <register type="IEventBus`2[IEventLong,long]" mapTo="TestEventBus`2[IEventLong,long]"></register>
    <register type="ISender" mapTo="TestSender">
      <constructor>
        <param name="eventBus" dependencyType="IEventBus`2[IEventLong, long]" />
      </constructor>
    </register>
  </container>
</unity>