Reflection Example 1 ----- execute the reflected DLL method and obtain the attribute value

Source: Internet
Author: User

Project Structure:

650) This. width = 650; "Title =" spximage70.jpg "src =" http://s3.51cto.com/wyfs02/M00/4D/C0/wKioL1RY5vzQLpNHAACCAAp6Bew937.jpg "alt =" wkiol1ry5vzqlpnhaaccaap6bew937.jpg "/>

Place the DLL file generated by the document library in the consoleapplication2 \ consoleapplication2 \ bin \ debug folder.

--------------------------------------------------------------- Document. CS

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace document {public class document {public string name = ""; Public String getname (string name) {return name ;}}}

--------------------------------------------------------------- Program. CS

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. reflection; using system. io; namespace consoleapplication2 {class program {static void main (string [] ARGs) {// obtain Assembly ASB = assembly. loadfrom (directory. getcurrentdirectory () + "/document. DLL "); // obtain the document class type document = ASB under the Assembly. getType ("document. document "); // instantiate the document class Object (if there is a parameter, you need to pass the object parameter) object docobj = activator. createinstance (document); // obtain the method methodinfo MI = Document in the document class. getmethod ("getname"); // parameter object [] parameter = new object [] {"James"}; console. writeline (MI. invoke (docobj, parameter ). tostring (); // return value of the output method: Zhang San // obtain the attribute fieldinfo Pi = Document in the document class. getfield ("name"); console. writeline (Pi. getvalue (docobj); // attribute of the output method: Li Si console. readkey ();}}}

This article is from the "programmer's home-Hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1571868

Reflection Example 1 ----- execute the reflected DLL method and obtain the attribute value

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.