public
static
WindsorContainercontainer
=
new
WindsorContainer(
@"
../../config.xml
"
);
container.AddComponent(
"
FormatService
"
,
typeof
(FormatService));
container.AddComponent(
"
format
"
,
typeof
(IMessageFormat),
typeof
(HtmlMessage));
4.2 使用XML文件配置
<?
xmlversion="1.0"encoding="utf-8"
?>
<
castle
>
<
components
>
<
component
id
="FormatService"
type
="IoCSample.FormatService,IoCSample"
/>
<
component
id
="format"
service
="IoCSample.Components.IMessageFormat,IoCSample"
type
="IoCSample.Components.HtmlMessage,IoCSample"
/>
</
components
>
</
castle
>
如何配置IList,IDictionary,Array等复杂类型
IDictionary
代码:
public
class
C
{
public
C()
{}
public
C(IDictionaryd)
{
this
.dictionary
=
d;
}
private
IDictionarydictionary;
public
IDictionaryDictionary
{
get
{
return
this
.dictionary;}
}
}
配置:
<
component
id
="c"
type
="CastleDemo.C,CastleDemo"
>
<
parameters
>
<
d
>
<
item
keyType
="System.String"
valueType
="System.String"
>
<
item
key
="a"
>
a
</
item
>
<
item
key
="b"
>
b
</
item
>
</
item
>
</
d
>
</
parameters
>
</
component
>
IList
代码:
public
class
D
{
private
IListlist;
public
D()
{}
public
IListList
{
get
{
return
this
.list;}
}
public
D(IListlist)
{
this
.list
=
list;
}
}
配置:
<
component
id
="d"
type
="CastleDemo.D,CastleDemo"
>
<
parameters
>
<
list
>
<
item
type
="System.String"
>
<
item
>
a
</
item
>
<
item
>
b
</
item
>
<
item
>
c
</
item
>
</
item
>
</
list
>
</
parameters
>
</
component
>
Array
代码:
public
class
E
{
private
int
[]ages;
public
int
[]Ages
{
get
{
return
this
.ages;}
}
public
E()
{
}
public
E(
int
[]ages)
{
this
.ages
=
ages;
}
}