反射 根据字段名获取字段值
/// <summary> /// 根据字段名称获取字段值 /// </summary> /// <param name="fieldName"></param> /// <param name="obj"></param> /// <returns></returns> public static string GetModelValue(string fieldName, object obj) { try { Type ts = obj.GetType();
//字段名不区分大小写 object o = ts.GetProperty(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase)?.GetValue(obj, null); string value = Convert.ToString(o); if (string.IsNullOrEmpty(value)) return null; return value; } catch { return null; } }