5

我已经更新到结构图 3,现在我不能使用 FillAllPropertiesOfType 进行 setter 注入。

它是否已弃用,我应该改用什么?

4

1 回答 1

5

我刚刚遇到了同样的问题。看起来这样做的新方法是 via Registry.PoliciesExpression

public interface IInjectable
{
    string Test();
}

public class Injectable : IInjectable
{
    public string Test()
    {
        return this.GetType().ToString();
    }
}

public class InjectTarget
{

    public IInjectable Injectable
    {
        get;
        set;
    }

}

static class Program
{
    static void Main()
    {

        ObjectFactory.Configure(x =>
        {
            //Setter injection
            x.Policies.FillAllPropertiesOfType<IInjectable>().Use<Injectable>();

            //Standard registration
            x.For<IInjectable>().Use<Injectable>();
            x.For<InjectTarget>().Singleton().Use<InjectTarget>();
        });

        var test = ObjectFactory.GetInstance<InjectTarget>();

        Console.WriteLine(test.Injectable.Test()); //WindowsFormsApplication3.Injectable
    }
}
于 2014-04-10T01:21:41.953 回答