spring.net (3)依赖注入基础2

来源:转载

如何调用其他对象的成员

接上文例子:

现在Person有一个属性 name  静态属性 eye 域 gender

 public class Person

{

public string name { get; set; }

public static object eye { get; set; }

public string gender;

public Pet pet { get; set; }public object seefrisbee(object color)

{

return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);

}

~Person()

{

Console.WriteLine(name + "离开");

}

}

对属性赋值:

 <object id="person" type="SpringDemo.Person,SpringDemo" singleton="true" >

<property name="pet" ref="dog" ></property>

<property name="name" value="Yahue"></property>

<property name="gender" value="男"></property>

<property name="eye" value="忧郁的眼神"></property>

<listener event="frisbeefly" method="seefrisbee">

<ref object="dog"/>

</listener>

</object>

name

 <object id="personname" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">

<property name="TargetObject" ref="person"></property>

<property name="TargetProperty" value="name"></property>

</object>

eye

 <!--静态属性的调用-->

<object id="personeye" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">

<property name="StaticProperty" value="SpringDemo.Person.eye"></property>

</object>

gender

<object id="persongender" type="Spring.Objects.Factory.Config.FieldRetrievingFactoryObject, Spring.Core">

<property name="TargetObject" ref="person"/>

<property name="TargetField" value="gender"/>

</object>

执行:

 Console.WriteLine(ctx.GetObject("personname"));

Console.WriteLine(ctx.GetObject("persongender"));

Console.WriteLine(ctx.GetObject("personeye"));

Console.ReadLine();

输出结果:

Yahue

拥有迷离而又忧郁的眼神

 

调用其他对象的方法:

Person有了静态say的方法:

 public class Person

{

public string name { get; set; }

public static object eye { get; set; }

public string gender;

public Pet pet { get; set; }

public static string say(string word1,string word2,string word3)

{

return string.Format("说:{0}-{1}-{2}", word1, word2, word3);

}

public object seefrisbee(object color)

{

return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);

}

~Person()

{

Console.WriteLine(name + "离开");

}

}

xml 

<object id="personsay" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">

<property name="TargetType" value="SpringDemo.Person,SpringDemo"></property>

<property name="TargetMethod" value="say"></property>

<property name="Arguments">

<list>

<value>啊</value>

<value>大海啊</value>

<value>全是水</value>

</list>

</property>

</object>

Console.WriteLine(ctx.GetObject("personsay"));

输出:

说:啊-大海啊-全是水

现在有个非静态的say2方法:

 public class Person

{

public string name { get; set; }

public static object eye { get; set; }

public string gender;

public Pet pet { get; set; }

public static string say(string word1,string word2,string word3)

{

return string.Format("说:{0}-{1}-{2}", word1, word2, word3);

}

public string say2(string word1, string word2, string word3)

{

return string.Format("说:{0}-{1}-{2}", word1, word2, word3);

}

public object seefrisbee(object color)

{

return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);

}

~Person()

{

Console.WriteLine(name + "离开");

}

}

 <object id="personsay2" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">

<property name="TargetObject" ref="person"></property>

<property name="TargetMethod" value="say2"></property>

<property name="NamedArguments">

<dictionary>

<entry key="word1" value="啊!"></entry>

<entry key="word2" value="大海啊!"></entry>

<entry key="word3" value="全是水!"></entry>

</dictionary>

</property>

</object>

注意ref的值是待调用对象的实例

调用:

 Console.WriteLine(ctx.GetObject("personsay2"));

执行结果:

说:啊!-大海啊!-全是水!

注意:方法参数的传递有两种方式:NamedArguments和Arguments 前者方法名和值是个键值,后者是个注入的参数顺序和方法的参数顺序对应的集合。


分享给朋友:
您可能感兴趣的文章:
随机阅读: