Simple reflection mechanism

Source: Internet
Author: User

1. What is reflection?

MSND: Reflection provides objects (type types) that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If you use attributes in your code, you can use reflection to access them.

reflected namespaces: System.Reflection, specific reference http://msdn.microsoft.com/zh-cn/library/system.reflection.aspx

Small compilation Understanding: In the process of doing, generally take the class instantiation, to manipulate the behavior of the object. Is that we have to create the object before we can take advantage of the object's behavior, and reflection is in the running state, to know all the properties and methods of the class, and this dynamically acquired information and the ability to dynamically invoke the method of the object is called reflection.


2. Introduction of DLL Operation class

The introduction of DLLs or namespaces in a program is a way to manipulate classes by introducing compiled DLLs to see an example.

DLL written by myself

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >namespace test{public    class Test    {public        int Add (int a,int b)        {            return a + B;        }}    } </span>


After compiling the above class, we can see the compiled DLL in the bin\debug of the project and add the reference to the other project.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >namespace aa{    class program    {        static void Main (string[] args)        {            test.test a = new Test.test (); C7/>console.writeline (A.add (3, 4));}}}    </span>

When a compiled DLL is introduced, it is possible to manipulate the behavior of classes encapsulated in DLLs by instantiating classes as objects.


2. Dynamic creation of instances of classes using reflection

Condition: We create two class libraries to demonstrate the operation.

Add Class: We need to test the class by reflection

Program class: the console Application.


Add Class

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >namespace add{    class Add {//<summary>///Add///</summary>//        < Param name= "A" > Parameter I </param>//        <param name= "B" > Parameters two </param>//        <returns></ returns> public        int Jia (int a, int b)        {            return a + b;}}    } </span>


Program Class

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using Test;using system.reflection;//Reflection namespace namespace aa{    class program    {        static void Main (string[] args) The        {                   //reflection mechanism dynamically creates an instance of the class            Add.add b=assembly.load ("Add"). CreateInstance ("Add.add") as Add.add;            Console.WriteLine (B.jia (4, 9));}}}    </span>

the implementation is particularly simple, mainly using the Assembly class for some methods, more methods, please refer to MSDN.


3. configuration file Resolution Create class library operations

A demo instance that was recently done to dynamically create a class library in an assembly through the reflection mechanism

Preconditions: DALMYSQL.dll: assembly;

Dalmssql. Dbsessionfactory: A factory class in the class library


Add-ons in configuration files

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > <appSettings>    <add key= "dbsessionfactory" value= "Dalmssql. Dbsessionfactory "/>    <!--Add a reference to the DLL, referring to the path >    <add key=" Dbsessionfactorydll "value=" C:\Users\ Zhou\desktop\tree\mvcoa\mvcoa\mvcoa\bin\dalmysql.dll "/>  </appSettings></span>

Reflective Class Library objects

Program Test class

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > class program    {        static void Main (string[] args)        {            //1. read Config file             string strfactorydll = system.configuration.configurationmanager.appsettings["Dbsessionfatorydll"];             String strfactorytype = system.configuration.configurationmanager.appsettings["Dbsessionfatory"];              2. Loading assembly                    Assembly Daldll = Assembly.LoadFrom (strfactorydll);            Gets the type of class to instantiate in the assembly type            typedbsessionfactory = Daldll.gettype (strfactorytype);            The reflection object needs to be cast            idal. Idbsessionfactory sessionfactory = Activator.CreateInstance (typedbsessionfactory) as IDAL. idbsessionfactory;        }    } </span>


Analysis: The same features are used, mainly assembly assemblies to load reflection.


------------------------------- Welcome Reviews ---------------------------------------------------------------







Simple reflection mechanism

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.