誠如他第22樓“只因渴求等待”提出的疑問一樣,他的下面那一段代碼是存在一點點問題的,
XElement root = XElement.Load(fileName); var objects = from obj in root.Elements("object") select obj;如果照搬照抄劉冬大俠的這段代碼那是不會成功讀取數據的,竊以為這應該是劉冬大俠故意埋的一雷吧。
根據他的文章,我實踐了一遍:
先創建了幾個類,一個Person類; 一個Man類; 一個Woman類,一共3個類,后面會將根據這個三個類創建xml文檔;
Person:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IocEasy{ public class Person { public string Name { get; set; } public string Sex { get; set; } public Person() { } public void Eat(string something) { Console.WriteLine(something); } public void MakeLove(Person person) { switch (person.Sex) { case "男": Console.WriteLine(this.Name + "和" + person.Name + "只能搞基"); break; case "女": Console.WriteLine(this.Name + "和" + person.Name + "可以相愛"); break; default: break; } } }}Man:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IocEasy{ public class Man : Person { public Man() { } public Man(string name, string sex) { base.Name = name; base.Sex = sex; } }}Woman:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IocEasy{ public class Woman : Person { public Woman() { } public Woman(string name, string sex) { base.Name = name; base.Sex = sex; } }}接下來就根據上面三個類(隨手寫的)創建xml文檔,
Object.xml:
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object id="Person" type="IocEasy.Person,IocEasy" ></object> <object id="Man" type="IocEasy.Man,IocEasy" ></object> <object id="Woman" type="IocEasy.Woman,IocEasy" ></object></objects>
跟著就是XmlFcatory類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Linq;using System.Data;using System.Xml;namespace IocEasy{ public class XmlFactory { private IDictionary<string, object> objectDefine = new Dictionary<string, object>(); public XmlFactory(string fileName) { InstanceObjects(fileName); } private void InstanceObjects(string fileName) { XNamespace ns = "http://www.springframework.net"; XName name = ns + "object"; XElement root = XElement.Load(fileName); var objects = from obj in root.Elements(name) select obj; objectDefine = objects.ToDictionary( k => k.FirstAttribute.Value, v => { string typeName = v.Attribute("type").Value; Type type = Type.GetType(typeName); return Activator.CreateInstance(type); } ); } public object GetObject(string id) { object result = null; if (objectDefine.ContainsKey(id)) { result = objectDefine[id]; } return result; } }}最后在就是主程序入口處調用了:
Program類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IocEasy{ class Program { static void Main(string[] args) { AppRegister(); Console.ReadLine(); } static void AppRegister() { XmlFactory ctx = new XmlFactory(@"C:/Documents and Settings/Administrator/桌面/IocEasy/IocEasy/Object.xml"); Person p1 = ctx.GetObject("Man") as Person; p1.Name = "Euler"; p1.Sex = "男"; Person p2 = ctx.GetObject("Woman") as Person; p2.Name = "Echo"; p2.Sex = "女"; p1.Eat(p1.Name + "喜歡抽煙"); p2.Eat(p2.Name + "喜歡旅行"); p1.MakeLove(p2); } }}這是一個完整的實踐,只不過其中的謬誤稍作修改罷了。
修改的代碼如下:
private void InstanceObjects(string fileName) { XNamespace ns = "http://www.springframework.net"; XName name = ns + "object"; XElement root = XElement.Load(fileName); var objects = from obj in root.Elements(name) select obj; objectDefine = objects.ToDictionary( k => k.Attribute("id").Value,//k.FirstAttribute.Value, v => { string typeName = v.Attribute("type").Value; Type type = Type.GetType(typeName); return Activator.CreateInstance(type); } ); }如此才能正常讀取數據。
新聞熱點
疑難解答