C#_assembly-reflection_ Assembly and reflection

Source: Internet
Author: User

Assembly:

The concept of the 1..net. exe. DLL (available for loading by itself cannot perform a no-entry function main function)

2. Include

Type metadata (the type defined in the code)

Assembly metadata (describes the assembly itself)

Il code

Resource file

Benefits of using assemblies: one is to reduce the size of the program itself (because you can call only the DLLs you need) module Two is the encapsulation code to provide an interface

--How to add a reference to an assembly: Add path, project reference, GAC (global assembly Cache)//cannot loop Add Reference//c# can invoke DLL files written by other programs

---------------------------------Reflection--------------------------------------------------------------------

Reflection: Can be understood as a technical means to get the type meta-data

Create an object directly from a. dll, invoke a member

We first create the main program in the same project, and then build another assembly (under the same project)

Here Testclasslibrary is a class library to add some types of classes in Class1.cs AH interface ah commissioned ah what:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespacetestclasslibary8 {9      Public classClass1Ten     { One  A     } -  -     Internal classPrivatemyclass the     { -          -     } -  +      Public class Person -     { +          Public stringName {Get;Set; } A          Public intAge {Get;Set; } at          Public stringEmail {Get;Set; } -  -     } -  -      Public Delegate voidmydelegate (); -  in      Public Interfaceiflyable -     { to         voidFly (); +     } -  the}
Class Library

Then right-click Generate to find the DLL file path under the Debug directory:

F:\Cheng_20140819\VS2012_WorkSpace\_Assembly\Assembly\TestClassLibary\bin\Debug\TestClassLibrary.dll

This can be dynamically loaded in our main program. ^_~

Jiangzi

1     #regionTo get a type in another assembly2             //Dynamically loading Assemblies3Assembly-Assembly.loadfile (@"F:\Cheng_20140819\VS2012_WorkSpace\Assembly\Assembly\TestClassLibary\bin\Debug\TestClassLibary.dll");4 5             //GetType () here is equivalent to TypeOf (Assembly) cannot get all the types in all assemblies6 7             //use GetTypes to get all types of public internal can get8type[] ts =The . GetTypes ();9             foreach(Type Iteminchts)Ten             { One Console.WriteLine (item. Name); A             } -  -             //get only the types that are exposed the             //get the type of export -type[] Publictypes =The . Getexportedtypes (); -             foreach(Type Iteminchpublictypes) -             { +Console.WriteLine ("--->>"+item. Name); -             } +             #endregion

Of course, if we want to get the specified type call Overload:

Assembly assembly.loadfile (@ "F:\Cheng_20140819\VS2012_WorkSpace\20151129_Heima_Day11_Assembly\Assembly\ Testclasslibary\bin\debug\testclasslibary.dll ");

---------------------------------------------here assume that there is an Add method in the person class that gets the return value of two parameters after adding a method, remember to regenerate

Gets the specific type
Type PersonType =.   GetType ("testclasslibary.person"); The type requires a fully qualified name: Remember to bring a namespace yo ~

To create an object based on a specific type
Object personobj = Activator.CreateInstance (PersonType);

Get a specific method
MethodInfo method = Persontype.getmethod ("Add");

Call
Object Obj= method.    Invoke (Personobj, new object[] {2, 3}); Parameter 1 represents the object of the obtained type, and Parameter 2 represents the parameter list of the called method

--------------------------------Call Property---------------------

1Assembly-Assembly.loadfile (@"F:\Cheng_20140819\VS2012_WorkSpace\Assembly\Assembly\TestClassLibary\bin\Debug\TestClassLibary.dll");2 3Type t =. GetType ("Testclasslibary.person");4 5             Objectobj =activator.createinstance (t);6 7PropertyInfo Pifo = T.getproperty ("Name");8 9Pifo. SetValue (obj,"Cheng");Ten  OneMethodInfo Mifo = T.getmethod ("Writename"); A  -Mifo. Invoke (obj,NULL);

--------------------------------use a specific constructor

1             #regionManually find the constructor and call the constructor to create the object of the type2Assembly-Assembly.loadfile (@"F:\Cheng_20140819\VS2012_WorkSpace\20151129_Heima_Day11_Assembly\Assembly\TestClassLibary\bin\Debug\ TestClassLibary.dll");3 4Type Persontype=ass. GetType ("Testclasslibary.person");5 6           //Use ctor GetConstructor () whose argument list is a list of the specified constructors7ConstructorInfo ctor= Persontype.getconstructor (NewType[] {typeof(string),typeof(int),typeof(string)});8             ObjectO = ctor. Invoke (New Object[] {"CCC", -,"[email protected]" });9 Console.WriteLine (o.tostring ());TenMethodInfo MInfo = Persontype.getmethod ("Writename"); OneMinfo.invoke (O,NULL); A  -             #endregion

  

C#_assembly-reflection_ Assembly and reflection

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.