///如未特别说明,本人所发表的技术文章都为原创, 任何人引用都请包含本声明
///作者: CSDN网名alias88,邮件:alias88@163.com,QQ:63343 ,欢迎加我
/// <summary>
///依导航路径调用对象成员方法,或返回对象其子、孙属性或字段成员.
///例子:
///获得表的第二列的名称
///GetMember(dataTable1.GetType(),"Columns.Item.Name",dataTable1,1)
///执行表的Select方法,并返回可能的DataRow数组
///GetMember(dataTable1.GetType(),"Select",dataTable1,"客户编号 = 'C0001')
///取活动表单的名称,静态成员不需传入obj
///GetMember(typeof("System.Windows.Forms.Form"),"ActiveForm.Name",null,null)
/// </summary>
/// <param name="type">获得此类型的子孙成员</param>
/// <param name="memberPath">子孙成员的路径描述,以点作为分隔符</param>
/// <param name="obj">如果成员是静态类型,则为null</param>
/// <param name="args">参数,注意此参数只能为memberPath最后一个成员使用</param>
/// <returns></returns>
public static object GetMember(Type type,string memberPath,object obj,object[] args)
{
if (memberPath==null || memberPath.Trim ()=="")
throw new ArgumentException ("缺少必需的参数","memberPath");
if (type==null )
throw new ArgumentException ("缺少必需的参数","type");
Type _objectType=type;
object _object=obj;
object[] paras=null;
string[] strMember=memberPath.Split ('.');
for (int i=0; i<strMember.Length ;i++)
{
string MemberName=strMember[i];
if (i>=strMember.Length-1 )
paras=args;
_object = _objectType.InvokeMember(MemberName,
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.GetField | BindingFlags.GetProperty |
BindingFlags.InvokeMethod | BindingFlags.IgnoreCase ,
Type.DefaultBinder, _object, paras);
if (_object!=null)
_objectType=_object.GetType ();
else if (i<strMember.Length-1)
throw new ArgumentNullException(MemberName);
}