我的演示代码很简单工厂模式与统一集结<T>麻烦
using Microsoft.Practices.Unity;
using System;
public interface IDecorator
{
string GetA();
}
public class Decorations:IDecorator
{
public string GetA()
{
return "temp";
}
}
public class Base
{
}
public class Derive : Base
{
[Dependency]
public IDecorator DerivedDecorations { get; set; }
}
public class Program
{
private static void Main(string[] args)
{
Base bd = new Derive(); // here is the point
var container = new UnityContainer();
container.RegisterType<IDecorator, Decorations>();
container.BuildUp(bd); // bd.DerivedDecorations is null
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
在派生类中的DerivedDecorations
无法在上述情况下
得到解决,如果我们的代码更改为Derive bd = new Derive();
没有问题
我不清楚原因,因为我们使用工厂模式,有没有人能给我一些原因呢?
为什么你不只是'导出temp = new Derive(); container.BuildUp(temp);导出bd = temp; ' –
因为我们使用工厂....代码就像Base b = GetDerived().... – allencharp