Attempt: Read the node from XML and dynamically create an object.
Reference: http://www.cnblogs.com/Arlen/archive/2007/05/30/765506.html
In factoryCode:
Public Class Publicsoftwareconfigfactory
{
Protected Hashtable item = New Hashtable ();
Public Ipublicsoftwareconfig This [Publicsoftwareconfigtype]
{
Get
{
If (Item. containskey (type. tostring ()))
Return (Ipublicsoftwareconfig) item [type. tostring ()];
Else
Return Null ;
}
}
Public Publicsoftwareconfigfactory ( String Srcxml)
{
Xmldocument Doc = New Xmldocument ();
Doc. loadxml (srcxml );
Xmlnode Root = Doc. documentelement;
// Extract objects from XML
Foreach (Publicsoftwareconfigtype softwaretype In Enum. getvalues ( Typeof (Publicsoftwareconfigtype )))
{
Xmlnode softwarenode = Root. selectsinglenode (softwaretype. tostring (). tolower ());
If (Softwarenode = Null )
Continue ;
Assembly = Assembly. loadfrom ( @" E: \ webapp \ helper \ arlenliu_configureproj_5_int \ dsecu_moniter_vob \ configure \ configentity \ bin \ debug \ Tencent. itil. config. configentity. dll " );
Type softwareclass = Assembly. GetType ( " Tencent. itil. config. configentity. publicsoftwareconfig. " + Softwaretype. tostring ());
// Object softwareobj = activator. createinstance (softwareclass );
Object Softwareobj = Assembly. createinstance (softwareclass. fullname );
Propertyinfo [] proparr = Softwareclass. getproperties (bindingflags. Public | Bindingflags. Instance | Bindingflags. flattenhierarchy );
For ( Int I = 0 ; I < Proparr. length; I ++ )
{
Xmlnode propnode=Softwarenode. selectsinglenode (proparr [I]. Name. tolower ());
Proparr [I]. setvalue (softwareobj, propnode. innertext,Null);
}
If ( ! Item. containskey (softwaretype. tostring ()))
Item. Add (softwaretype. tostring (), softwareobj );
}
}
}
Business logic: Public Static Void Modifyprocessforpublicsoftwareconfig ( String Soruce, String Operator, String Deivceid, String Softwarexmlstr)
{
// Extraction of public software configuration classes based on software configuration information XML
Publicsoftwareconfigfactory softwarefactory = New Publicsoftwareconfigfactory (softwarexmlstr );
Ipublicsoftwareconfig OBJ = Softwarefactory [publicsoftwareconfigtype. kernel];
Kernel = (Kernel) OBJ;
}
In this section, the kernel = (kernel) OBJ; error prompts that the conversion is not allowed.
This ipublicsoftwareconfig is defined in another DLL, and all entity classes in the Entity inherit from it. Only in this way can it be converted to ipublicsoftwareconfig, but it still cannot be converted to entity objects.
There is a solution: declare the attributes of all possible sub-classes of this interface in the interface. (Obviously, this method is not scientific .)
Finally, we use deserialization to solve this problem.
Everything cannot be too rough.