Two classes:
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 "constructor parameter Intpro" + Intpro;
}
}
Objects.xml file
<?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= "></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)
{
Instantiate an IOC container
Createcontextcontainer (filename);
Attribute Injection
Propertyzhuru (filename);
}
public static objectfactory intance (string filename)
{
if (objfinstance==null)
{
Lock (Lochelper)
{
Objfinstance=objfinstance?? New objectfactory (filename);//If objfinstance is null, it is instantiated
}
}
return objfinstance;
}
<summary>
Instantiate an IOC container
</summary>
<param name= "filename" >objects.xml filename </param>
private void Createcontextcontainer (string filename)
{
XElement xmlroot = xelement.load (filename);
var objects = from obj in XmlRoot. Elements ("Object")
Select obj;
Get parameterless constructs
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);//Get Class object types
return Activator.CreateInstance (type);
}
);
Get parametric constructs
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);
Returns the value of a parameter of the specified type
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>
//attribute injection
//</SUMMARY>
//<param name= "filename" >objects.xml file name & Lt;/param>
private void Propertyzhuru (string filename)
{
xelement xmlroot = Xele ment. Load (filename);
var objects = from obj in XmlRoot. Elements ("Object")
select obj;
foreach (keyvaluepair<string,object> ob in ObjectContext)
& nbsp {
foreach (Var el in objects. Where (c = c.attribute ("id"). Value = = ob. Key). Elements ("property")//Get the element attribute collection of the key stored in the dictionary under object ID in XML
{
Type type = ob. Value.gettype ();//Gets the type of the current object
foreach (var para in type. GetProperties ())//All properties of the current object
{
//Assigning a property value to a class object saved in a dictionary
&NBS P 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);//Set a new value for the property of the specified instance
}
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>
Get Instance Object
</summary>
<param name= "id" ></param>
<returns></returns>
public Object GetObject (string id)
{
Object result = null;
if (Objectcontext.containskey (ID))
{
result= Objectcontext[id];
}
return result;
}
}
Test:
Objectfactory factory = objectfactory.intance (@ "f:\ cycle test \sringnetjykuangjia\springnet\objects.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 ();
Test results:
Original reference from: http://www.cnblogs.com/GoodHelper/archive/2009/11/02/1594398.html