How to Use reflection to dynamically create objects and the type cannot be converted

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.