c#

位置:IT落伍者 >> c# >> 浏览文章

C#反射取值 源代码


发布日期:2018年10月17日
 
C#反射取值 源代码

/// <summary> /// 获取对象中指定name的值 贾世义/// </summary> /// <param name=obj>DataRowView和实体类对象</param> /// <param name=name>字段或类成员</param> /// <returns></returns> public static object GetValue(object obj string name)

{ if (obj == null || StringIsNullOrEmpty(name))

{ return null} //DataRow优先if (obj is DataRowView || obj is DataRow)

{ return DataHelperGetValue(obj name)} //键值集合if (obj is NameValueCollection)

{ return ((NameValueCollection)obj)[name]} //实现了IDictionary接口的类if (objGetType()GetInterface(IDictionary true) != null)

{ return ((IDictionary)obj)[name]} //类反射int p = nameIndexOf(if (p ==

{ int ps = nameIndexOf(if (ps ==

{ //属性PropertyInfo pInfo = objGetType()GetProperty(name)if (pInfo != null)

{ return pInfoGetValue(obj null)} //字段FieldInfo fInfo = objGetType()GetField(name)if (fInfo != null)

{ return fInfoGetValue(obj)} //方法MethodInfo mInfo = objGetType()GetMethod(name)if (mInfo != null)

{ return mInfoInvoke(obj null)} else { return null} else { //带参数方法int pe = nameIndexOf(if (pe ==

{ pe = nameLength} MethodInfo mInfo = objGetType()GetMethod(nameSubstring( ps))if (mInfo != null)

{ return mInfoInvoke(obj DataHelperGetStrings(nameSubstring(ps + pe ps Replace( )))} else { return null} else { //包含子类string name = nameSubstring( p)object obj = nullPropertyInfo pInfo = objGetType()GetProperty(nameif (pInfo != null)

{ obj = pInfoGetValue(obj null)} else { FieldInfo fInfo = objGetType()GetField(nameif (fInfo != null)

{ obj = fInfoGetValue(obj)} if (obj == null)

{ return null} else { return GetValue(obj nameSubstring(p + ))}

               

上一篇:ADO.NET:通向未来之桥

下一篇:Microsoft.NET Framework 3.0版本发布