Gets the value of the attribute in the class////////public string Getmodelvalue (string FieldName, Object obj) {try {Type Ts = obj. GetType (); Object o = Ts.getproperty (FieldName). GetValue (obj, null); String Value = Convert.ToString (o); if (string. IsNullOrEmpty (Value)) return null; return Value; } catch {return null; }}//////Set attribute values in class/////////public bool Setmodelvalu E (String fieldname,string Value, object obj) {try {Type Ts = obj. GetType (); Object v = Convert.changetype (Value, Ts.getproperty (FieldName). PropertyType); Ts.getproperty (FieldName). SetValue (obj, V, null); return true; } catch { return false; } }
Found on the internet did not find, just wrote a method for sharing.
There's a thing here that's been done for a long time in writing the method. That is, if the property type is int, the incoming string string is set unsuccessfully.
Here I use the Convert.changetype conversion, which is automatically converted according to the attribute type.
Transferred from: http://blog.csdn.net/cestarme/article/details/6548126
[Go] C # reflection Set property values and get property values