/// <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(name)if (pInfo != null)
{ obj = pInfoGetValue(obj null)} else { FieldInfo fInfo = objGetType()GetField(name)if (fInfo != null)
{ obj = fInfoGetValue(obj)} if (obj == null)
{ return null} else { return GetValue(obj nameSubstring(p + ))}
From:http://tw.wingwit.com/Article/program/net/201311/13665.html