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