Two ways to match XML to object collection in C #

Source: Internet
Author: User
Tags reflection

First, the preface

The previous essay mainly converts the entity into the corresponding XML or XML object, and does not take into account attribute attributes, and then has time to tidy up. The XML match in this paper involves the attributes of the class and the attributes of the attribute, and optimizes the matching of the class, minimizing the performance problems caused by reflection (in fact, the performance is negligible for an XML match that is not a lot of objects).

Second, class diagram design

The main idea is to match the node of the class name by reflection, and then match the value of the attribute (property attribute name or attribute name), as shown in the design:

The functions of each class in a class diagram are as follows:

PropertyAttribute, ClassAttribute, Stringextension, funcdictionary the role of the XmlAttribute and the entity of the conversion and matching program (with source code).

Attributeutility is primarily used to get class names and property names.

Reflectionutility is primarily used to obtain a dictionary of the property objects of the type and the attribute names corresponding to the attributes, and to optimize the processing of the matching.

Xmlparser is primarily used to match XML to entity objects.

Third, the concrete realization

3.1 reflectionutility

This class is used primarily to get a dictionary of the attribute names and corresponding property objects of a type. The returned type is dictionary<string, Propertyinfo>, where key is the attribute name of the property, not the property name, and Vaule is the corresponding Property object. This design is primarily convenient for subsequent operations.

The main methods are:

public static dictionary<string, propertyinfo> getpropertymapperdictionary<t> () where t:new (),

public static dictionary<string, propertyinfo> getpropertymapperdictionary (type type), with the following code:

public class Reflectionutility {public static dictionary<string, propertyinfo> Getpropertymapperdictio
        Nary<t> () where T:new () {return getpropertymapperdictionary (typeof (T));
            public static dictionary<string, propertyinfo> getpropertymapperdictionary (type type) {
            if (type = = NULL) {return null; list<propertyinfo> properties = type. GetProperties ().
            ToList ();
            var namemapperdic = new dictionary<string, propertyinfo> ();
    
            string propertyattributename = null; foreach (PropertyInfo property in properties) {Propertyattributename = ATTRIBUTEUTILITY.GETPR
                Opertyname (property);
            Namemapperdic.add (Propertyattributename, property);
        return namemapperdic; } public static ilist<string> GetpropertynAmes<t> () where T:new () {list<propertyinfo> properties = typeof (T). GetProperties ().
            ToList ();
    
            var propertynames = new list<string> (); foreach (PropertyInfo property in properties) {Propertynames.add (attributeutility.getproperty
            Name (property));
        return propertynames; }
    }

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.