C#通过反射设置属性值和获取属性值
发布时间:2022-09-22 16:41:29 160 相关标签:
// 获取类中的属性值
public string GetModelValue(string FieldName, object obj){
try
{
Type t = obj.GetType();
object o = t.GetProperty(FieldName).GetValue(obj, null);
string Value = Convert.ToString(o);
if (string.IsNullOrEmpty(Value)) return null;
return Value;
}
catch
{
return null;
}
}
// 设置类中的属性值
public bool SetModelValue(string FieldName,string Value, object obj){
try{
Type t = obj.GetType();
object v = Convert.ChangeType(Value, t.GetProperty(FieldName).PropertyType);
t.GetProperty(FieldName).SetValue(obj, v, null);
return true;
}
catch{
return false;
}
}
文章来源: https://blog.51cto.com/u_15604197/5681022
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报