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; }
}