Castle Windsor通过构造函数参数 [英] Castle Windsor passing constructor parameters

查看:329
本文介绍了Castle Windsor通过构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有几个属性的IAddress类。然后我有一个实现这个接口的具体类型。这个具体类型有一些我可以使用的不同的构造函数。如何在运行时将参数值传递给其中一个构造函数?我不能使用配置文件,因为我会重复使用这个具体类型多次,每次参数值将不同。

  IWindsorContainer container = new WindsorContainer(new XmlInterpreter()); 
IAddress address = container.Resolve< IAddress>();


public interface IAddress
{
string Address1 {get;组; }
string Address2 {get;组; }
string City {get;组; }
string State {get;组; }
string ZipCode {get;组; }
}


class TestAddress:IAddress
{

private string _address1;
private string _address2;
私人字符串_city;
私人字符串_countyName;
private string _state;
private string _zipCode;

public string Address1
{
get {return _address1; }
set {_address1 = value; }
}

public string Address2
{
get {return _address2; }
set {_address2 = value; }
}

public string City
{
get {return _city; }
set {_city = value;
}

public string State
{
get {return _state; }
set {_state = value;
}

public string ZipCode
{
get {return _zipCode; }
set {_zipCode = value; }
}

public string CountyName
{
get {return _countyName; }
set {_countyName = value;



public MelissaAddress(string address1,string address2,string city,string state,string zipcode)
{
Address1 = address1;
Address2 = address2;
城市=城市;
状态=状态;
ZipCode = zipcode;
}

public MelissaAddress(string address1,string address2,string zipcode):this(address1,address2,null,null,zipcode)
{}

public MelissaAddress(string address1,string address2,string city,string state):this(address1,address2,city,state,null)
{}
}


解决方案

可以使用 Resolve(object argumentsAsAnonymousType) Resolve(IDictionary参数)。 Windsor将选择最匹配的构造函数。



例如,这将选择您的第二个构造函数:

  container.Resolve< IAddress>(
new {address1 =myaddress1,address2 =myaddress2,zipcode =myzipcode}


I have an IAddress class with a few properties. I then have a concrete type that implements this interface. This concrete type has a couple of different constructors I could use. How can I pass parameter values to one of these constructors at run-time? I cannot use the config file as I will be reusing this concrete type multiple times and each time the parameter values will be different.

IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
IAddress address = container.Resolve<IAddress>();


public interface IAddress
{
    string Address1 { get; set; }
    string Address2 { get; set; }
    string City { get; set; }
    string State { get; set; }
    string ZipCode { get; set; }
}


class TestAddress : IAddress
{

    private string _address1;
    private string _address2;
    private string _city;
    private string _countyName;
    private string _state;
    private string _zipCode;

    public string Address1
    {
        get { return _address1; }
        set { _address1 = value; }
    }

    public string Address2
    {
        get { return _address2; }
        set { _address2 = value; }
    }

    public string City
    {
        get { return _city; }
        set { _city = value; }
    }

    public string State
    {
        get { return _state; }
        set { _state = value; }
    }

    public string ZipCode
    {
        get { return _zipCode; }
        set { _zipCode = value; }
    }

    public string CountyName
    {
        get { return _countyName; }
        set { _countyName = value; }
    }


    public MelissaAddress(string address1, string address2, string city, string state, string zipcode)
    {
        Address1 = address1;
        Address2 = address2;
        City = city;
        State = state;
        ZipCode = zipcode;
    }

    public MelissaAddress(string address1, string address2, string zipcode) : this(address1, address2, null, null, zipcode)
    { }

    public MelissaAddress(string address1, string address2, string city, string state) : this(address1, address2, city, state, null)
    { }
}

解决方案

You can use Resolve(object argumentsAsAnonymousType) or Resolve(IDictionary arguments). Windsor will select the best matching constructor.

For example this will select your second constructor:

container.Resolve<IAddress>(
    new {address1 = "myaddress1", address2 = "myaddress2", zipcode = "myzipcode"}
)

这篇关于Castle Windsor通过构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆