C # can read the field/method of a class by reflection, but how do I get the XML comment for that field?
Specific purpose: There is an entity class, the page needs to have a corresponding table, the style is generally
<TR> <TD>Ground longitude</TD> <TD> <inputID= ' Txt_longitude 'type= ' text 'class= ' Form-control 'name= ' Longitude '/></TD> <TD>Latitude</TD> <TD> <inputID= ' Latitude 'type= ' text 'class= ' Form-control 'name= ' Latitude '/></TD> </TR>
In fact, the body type, roughly:
< Summary > /// longitude </summary>public double? Longitude { get{return _longitude;} Set {this . Onpropertyvaluechange (_. Longitude, _Longitude
This._Longitude
}
Due to the large number of attributes in the entity class, it is very important to generate a page, so you want to read the XML comment and its attribute name of the entity through reflection, write a loop to generate the above page
After reviewing the relevant data, XML annotations are not written to the DLL, so it is not possible to get the XML annotations directly by reflection.
It is recommended that the XML file be parsed to get the XML comment
The omnipotent foreigner has already dealt with a similar problem, refer to Here
It provides a class library for processing and an example
1. Open the XML document annotation feature in the project properties in VS
is to generate an XML file named after the current project in the bin directory of the project, read the file, and get the XML comment
2. Get the XML comment as follows
XmlElement documentation = Docsbyreflection.xmlfrommember (typeof (Someexampleclass). GetProperty ("Exampleproperty")); Console.WriteLine (documentation["Summary"). Innertext.trim ());
Download Link:
Docsbyreflection_2
Using reflection to get the field name/field type/And its XML comments, you can generate page HTML as needed, as shown in the following example:
StringBuilder sb = new StringBuilder (); String rowtemp = "<TR>\ r \ n "+"<TD>{0}</TD>\ r \ n "+"<TD>" + "<inputID= ' Txt_{1} 'type= ' text 'class= ' Form-control 'name= ' {1} '/></TD>\ r \ n "+"<TD>{2}</TD>\ r \ n "+"<TD>\ r \ n "+"<inputID= ' {3} 'type= ' text 'class= ' Form-control 'name= ' {3} '/></TD>\ r \ n "+"</TR>\ r \ n "; Traversing basic properties, generating tablespropertyinfo[] Prolist = TP1. GetProperties (); List<string>Ignorelist = new List<string>(); Ignorelist.add ("Project_code"); BOOL Toend = false; #region Distinguishing field typesfor (int i = 0; i < prolist.length;) {
//The following loops are used to filter the fields, and a row of two <td>, so you need to retrieve the next available Field object, which appears to be compared around while (Ignorelist.contains (prolist[i). Name) {i++; if (i = = prolist.length) {toend = true; Break ; }} if (toend) {break; } PropertyInfo p1 = Prolist[i]; i++; PropertyInfo P2 = null; if (I < prolist.length) {while (Ignorelist.contains (prolist[i). Name) {i++; if (i = = prolist.length) {toend = true; Break ; }}} if (Toend | | I== prolist.length) {} else {P2= Prolist[i]; i++; } sb. Appendline ("<tr>");if (P2! = null) {string str1 = gethtml (TP1, p1); String str2 = gethtml (TP1, p2); Sb. Appendline (STR1); Sb. Appendline (STR2); } else {string str1 = gethtml (TP1, p1); Sb. Appendline (STR1); } sb. Appendline ("</TR>"); } #endregion Write2text (@ "C:\STD\" +TP1. name+ ". txt", SB); Console.WriteLine ("End"); Console.ReadLine ();
Public Static stringGetcommenttext (Type TP1,stringnm) {Console.WriteLine (nm); XmlElement Documentation=Docsbyreflection.xmlfrommember (TP1. GetProperty (nm)); Console.WriteLine (documentation["Summary"]. Innertext.trim ()); returndocumentation["Summary"]. Innertext.trim (); } Public Static voidWrite2text (stringfilename, StringBuilder sb) { using(FileStream fs =NewFileStream (filename, filemode.openorcreate)) { using(StreamWriter SW =NewStreamWriter (FS)) {SW. Write (sb.) ToString ()); Sw. Flush (); } } } Public Static stringgethtml (Type tp1,propertyinfo p) {if(P.name.trim () = ="conveyance_tunnel_import_elevation") Console.WriteLine (""); stringINPUTTD ="<td>{0}</td>\r\n"+"<td>\r\n"+"<input id = ' Txt_{1} ' type = ' text ' class= ' Form-control ' name= ' {1} '/>\r\n"+"</td>\r\n"; stringSELECTTD =@"<td>{0}</td> <td> <select id= ' {1} ' class= ' Form-control ' name= ' {1} ' > {2} </select> </td>"; stringCHKTD =@"<td>{0}</td> <td> <label for= ' Chky_{1} ' > is <input type= ' che Ckbox ' id= ' chky_{1} ' value= ' true ' class= ' Form-control ' name= ' {1} ' ></label> <label for= ' chkn _{1} ' > No <input type= ' checkbox ' value= ' false ' id= ' chkn_{1} ' class= ' Form-control ' name= ' {1} ' ></label> </td>"; if(P.propertytype = =typeof(Guid?) | | p.propertytype==typeof(Guid)) { //GUID type, go to the database to find and generate a selectStringBuilder OPTSB =NewStringBuilder (); DataTable DT= Sqlhelper.executedatatable ("SELECT * from foreign_key_table where [e-mail protected] and [email protected] ORDER by order_id", NewSqlParameter ("@cc", P.name),NewSqlParameter ("TC", Type_code)); if(dt. Rows.Count >0) { for(inti =0; i < dt. Rows.Count; i++) {Optsb.appendline (string. Format ("<option value= ' {0} ' >{1}</option>"Dt. rows[i]["Key_code"], dt. rows[i]["Key_name"])); } return string. Format (Selecttd, Getcommenttext (TP1, P.name), P.name, optsb.tostring ()); } Else { //if not found, the non-foreign key table is displayed directly return string. Format (Inputtd, Getcommenttext (TP1, p.name), p.name); } } Else if(P.propertytype = =typeof(BOOL?) || P.propertytype = =typeof(BOOL)) { return string. Format (Chktd,getcommenttext (TP1, p.name), p.name); } Else { //Generate input return string. Format (Inputtd,getcommenttext (tp1,p.name), p.name); } }
C # reading XML annotations