使用统一容器的开放通用类型的 PropertyInjection 抛出异常
PropertyInjection with open generic type using unity container throws exception
我正在尝试使用统一容器解析开放泛型类型。我收到 ResolutionFailed 异常。我正在以编程方式注册。无法理解 InitContainer 方法中出了什么问题。
更新:InitContainer 中的新 InjectionProperty("Age",25)) 导致异常。
使用 Unity 3.0 的代码
public abstract class Person<T> where T : class
{
protected T profession;
public Person(T profession)
{
this.profession = profession;
}
public abstract void WhoAreYou();
public int Age { get; set; }
}
public class Employee<T> : Person<T> where T : class
{
string personType;
public Employee(T profession, string personType) : base(profession)
{
this.personType = personType;
}
public override void WhoAreYou()
{
Console.WriteLine("I am " + personType);
Console.WriteLine("My age is " + Age);
Console.WriteLine("Profession" + typeof(T).ToString());
}
}
public abstract class Profession { }
public class Doctor : Profession { }
class Program
{
static void Main(string[] args)
{
var container = InitContainer();
var p = container.Resolve<Person<Doctor>>();
p.WhoAreYou();
Console.ReadKey();
}
static UnityContainer InitContainer()
{
UnityContainer container = new UnityContainer();
container.RegisterType(typeof(Person<>), typeof(Employee<>),
new ContainerControlledLifetimeManager() ,
new InjectionConstructor(new GenericParameter("T"), "Employee"),
new InjectionProperty("Age",25));
return container;
}
}
看来您确实在 Unity 中发现了一个错误!
仅当您尝试将 InjectionProperty
与继承 属性 的组合与开放仿制药注册结合使用时,才会出现此问题。此应该 是受支持的方案。我能够在 Unity 3.0、3.5 和 4.0(最新)上重现此问题。
以下任一方法解决了问题...
- 删除泛型
- 改为封闭式仿制药注册
- 将属性移动到具体的class
- 在基础class
的基础上使属性抽象或虚化
- 以不同于使用
InjectionProperty
的方式设置 属性
我正在尝试使用统一容器解析开放泛型类型。我收到 ResolutionFailed 异常。我正在以编程方式注册。无法理解 InitContainer 方法中出了什么问题。
更新:InitContainer 中的新 InjectionProperty("Age",25)) 导致异常。
使用 Unity 3.0 的代码
public abstract class Person<T> where T : class
{
protected T profession;
public Person(T profession)
{
this.profession = profession;
}
public abstract void WhoAreYou();
public int Age { get; set; }
}
public class Employee<T> : Person<T> where T : class
{
string personType;
public Employee(T profession, string personType) : base(profession)
{
this.personType = personType;
}
public override void WhoAreYou()
{
Console.WriteLine("I am " + personType);
Console.WriteLine("My age is " + Age);
Console.WriteLine("Profession" + typeof(T).ToString());
}
}
public abstract class Profession { }
public class Doctor : Profession { }
class Program
{
static void Main(string[] args)
{
var container = InitContainer();
var p = container.Resolve<Person<Doctor>>();
p.WhoAreYou();
Console.ReadKey();
}
static UnityContainer InitContainer()
{
UnityContainer container = new UnityContainer();
container.RegisterType(typeof(Person<>), typeof(Employee<>),
new ContainerControlledLifetimeManager() ,
new InjectionConstructor(new GenericParameter("T"), "Employee"),
new InjectionProperty("Age",25));
return container;
}
}
看来您确实在 Unity 中发现了一个错误!
仅当您尝试将 InjectionProperty
与继承 属性 的组合与开放仿制药注册结合使用时,才会出现此问题。此应该 是受支持的方案。我能够在 Unity 3.0、3.5 和 4.0(最新)上重现此问题。
以下任一方法解决了问题...
- 删除泛型
- 改为封闭式仿制药注册
- 将属性移动到具体的class
- 在基础class 的基础上使属性抽象或虚化
- 以不同于使用
InjectionProperty
的方式设置 属性