两个类:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonDao
{
private int intpro;
public PersonDao(int intpro)
{
this.intpro = intpro;
}
public Person Entity { get; set; }
public override string ToString()
{
return "构造函数参数intpro" + intpro ;
}
}
Objects.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<objects>
<object id="person" type="SpringNet.Person,SpringNet">
<property name="Name" value="danche"></property>
<property name="Age" value="24"></property>
</object>
<object id="personDao" type="SpringNet.PersonDao,SpringNet">
<constructor-arg name="intpro" value="1"></constructor-arg>
<property name="Entity" ref="person"></property>
</object>
</objects>
Factory:
public class ObjectFactory
{
public IDictionary<string, object> objectContext = new Dictionary<string, object>();
private static ObjectFactory objfinstance;
private static object locHelper = new object();
private ObjectFactory(string filename)
{
//实例化Ioc容器
CreateContextContainer(filename);
//属性注入
PropertyZhuru(filename);
}
public static ObjectFactory Intance(string filename)
{
if(objfinstance==null)
{
lock(locHelper)
{
objfinstance=objfinstance??new ObjectFactory(filename);//如果objfinstance为null就实例化
}
}
return objfinstance;
}
/// <summary>
/// 实例化IoC容器
/// </summary>
/// <param name="filename">Objects.xml文件名</param>
private void CreateContextContainer(string filename)
{
XElement xmlroot = XElement.Load(filename);
var objects = from obj in xmlroot.Elements("object")
select obj;
//获得无参数构造
objectContext = objects.Where(c => c.Element("constructor-arg") == null).ToDictionary(
k => k.Attribute("id").Value,
v => {
string typename = v.Attribute("type").Value;
Type type = Type.GetType(typename);//获得类对象的类型
return Activator.CreateInstance(type);
}
);
//获得有参数构造
foreach (var obj in objects.Where(c => c.Element("constructor-arg") != null))
{
string k = obj.Attribute("id").Value;
string typename = obj.Attribute("type").Value;
Type type = Type.GetType(typename);
//返回指定类型的参数的值
var args = from oo in type.GetConstructors()[0].GetParameters()
join el in obj.Elements("constructor-arg")
on oo.Name equals el.Attribute("name").Value
select Convert.ChangeType(el.Attribute("value").Value, oo.ParameterType);
objectContext.Add(k,Activator.CreateInstance(type,args.ToArray()));
}
}
/// <summary>
/// 属性注入
/// </summary>
/// <param name="filename">Objects.xml文件名</param>
private void PropertyZhuru(string filename)
{
XElement xmlroot = XElement.Load(filename);
var objects = from obj in xmlroot.Elements("object")
select obj;
foreach(KeyValuePair<string,object> ob in objectContext)
{
foreach (var el in objects.Where(c => c.Attribute("id").Value == ob.Key).Elements("property"))//获得xml中object下id为字典中保存的key的元素属性集合
{
Type type = ob.Value.GetType();//获得当前对象的类型
foreach(var para in type.GetProperties())//当前对象的所有所有属性
{
//给字典中保存的类对象的属性赋值
if(para.Name==el.Attribute("name").Value)
{
if(el.Attribute("value")!=null)
{
para.SetValue(ob.Value,
Convert.ChangeType(el.Attribute("value").Value,
para.PropertyType), null);//给指定实例的属性设置新值
}
else if(el.Attribute("ref")!=null)
{
if(objectContext.ContainsKey(el.Attribute("ref").Value))
{
var obj=objectContext[el.Attribute("ref").Value];
para.SetValue(ob.Value,obj,null);
}
}
}
}
}
}
}
/// <summary>
/// 获得实例对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public object GetObject(string id)
{
Object result = null;
if(objectContext.ContainsKey(id))
{
result= objectContext[id];
}
return result;
}
}
测试:
ObjectFactory factory = ObjectFactory.Intance(@"F:单车测试SringNetJYKuangjiaSpringNetObjects.xml");
PersonDao persondao = factory.GetObject("personDao") as PersonDao;
Console.WriteLine(persondao.Entity.Name);
Console.WriteLine(persondao.Entity.Age.ToString());
Console.WriteLine(persondao.ToString());
Console.ReadKey();
测试结果:
原文参考自:http://www.cnblogs.com/GoodHelper/archive/2009/11/02/1594398.html