0
我試圖從文件Unity.config加載Unity配置。 在接口的實現使用的通用論點統一配置文件 - GenericArguments違反類型參數約束
我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="TestUnity" />
<namespace name="TestUnity" />
<container>
<register type="IGeneric1`2[IGeneric2`1[long], long]" mapTo="ExampleGeneric`2[IGeneric2`1[long], long]">
</register>
</container>
</unity>
PROGRAMM代碼:
public interface IGeneric1<E, in Key> where E : IGeneric2<Key>
{
void Publish(E msg);
}
public interface IGeneric2<out Key>
{
Key SourceId { get; }
}
公共類ExampleGeneric:IGeneric1其中E:IGeneric2 { public void Publish(E msg) { throw new NotImplementedException(); } }
主:
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer().LoadConfiguration();
}
在行
IUnityContainer container = new UnityContainer().LoadConfiguration();
顯示錯誤:
GenericArguments[0] "TestUnity.IGeneric2`1[Key]", in "TestUnity.IGeneric1`2[E,Key]" violates the constraint of type paremeter "E"