如何調用其他對象的成員
接上文例子:
現在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 + "離開"); } }<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 前者方法名和值是個鍵值,后者是個注入的參數順序和方法的參數順序對應的集合。
新聞熱點
疑難解答