Public Static stringSortparam<t>(T t) {stringTstr =string. Empty; if(T = =NULL) { return string. Empty; } system.reflection.propertyinfo[] Properties= T.gettype (). GetProperties (System.Reflection.BindingFlags.Instance |System.Reflection.BindingFlags.Public); if(Properties. Length <=0) { return string. Empty; } foreach(System.Reflection.PropertyInfo IteminchProperties. (M = m.name))//compose a string after sorting { stringName =item. name;//Field nameObjectValue = Item. GetValue (T,NULL);//valueif(item. Propertytype.isvaluetype | | Item. PropertyType.Name.StartsWith ("String")) { if(Value! =NULL) Tstr+= (string. Format ("{0}{1}", name, value)); } Else{Sortparam (value);//Collection Field recursive}}//tstr = Tstr.orderby (m = + m). ToList (); returnTstr; }
PublicIlist<t>GetData (SqlDataReader reader) {IList<T> list =NewList<t>(); Type type=typeof(T); Propertyinfo[] Properties=type. GetProperties (); while(reader. Read ()) {T T= activator.createinstance<t>(); for(inti =0; I < properties. Length; i++) {Properties[i]. SetValue (t, Reader[i+1],NULL); } list. ADD (t); } returnlist; }
C # Traversal/Reflection Properties/Fields