如何在structuremap中进行setter注入3

本文关键字:setter 注入 structuremap | 更新日期: 2023-09-27 17:57:31

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

它被弃用了吗?我应该使用什么?

如何在structuremap中进行setter注入3

我刚刚遇到了同样的问题。看起来新的方法是通过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
    }
}