Use reflection to invoke an object member method based on the navigation path, or to return an object child, grandchild property, or field member

Source: Internet
Author: User
Tags reflection
Object
If not specifically stated, I published the technical articles are original, anyone reference please include this statement
Author: csdn net name alias88, mail: alias88@163.com,qq:63343, Welcome to add me



<summary>



Invokes the object member method, depending on the navigation path, or returns the child, grandchild property, or field member of the object.



Example:



Get the name of the second column of the table



GetMember (Datatable1.gettype (), "Columns.Item.Name", datatable1,1)



Executes the table's Select method and returns a possible DataRow array



GetMember (Datatable1.gettype (), "select", DataTable1, "customer number = ' C0001 ')



Takes the name of the active form, and static members do not need to pass in obj



GetMember (typeof ("System.Windows.Forms.Form"), "Activeform.name", Null,null)



</summary>



<param name= "Type" > Get this type of descendant member </param>



<param name= "Memberpath" The path description of the descendant members, with dots as delimiters </param>



<param name= "obj" > if the member is a static type, then null</param>



<param name= "args" > parameters, note that this parameter can only be used for Memberpath last member </param>



<returns></returns>



public static Object GetMember (Type type,string memberpath,object obj,object[] args)



{



if (Memberpath==null | | Memberpath.trim () = "")



throw new ArgumentException ("Missing required parameters", "Memberpath");



if (type==null)



throw new ArgumentException ("Missing required parameters", "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);



}



return _object;



}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.