Application of Net reflection in projects

Source: Internet
Author: User

Application of Net reflection in projects

The concept and basic principles of reflection are very detailed in msdn. This article mainly describes the application of reflection in my project.

One more concept of reflection is assembly, which can also be considered as a dll class library. assembly is a collection of all types. Another important thing is metadata. JIT uses metadata such as TypeRef and AssemblyRef of an assembly to determine the referenced assembly and its type. The metadata includes the name, version, language culture, and public key tag, JIT loads an assembly to the application domain based on the information. To load an Assembly by yourself, you can call the LoadXXX series method of the Assembly type. From Assembly, you can read the inheritance interfaces, methods, attributes, fields, and events of the classes in this dll.

 

Reflection is the required class to be dynamically created during the running process. The methods of interfaces and interfaces have been determined during compilation. The implementation of interfaces depends on their inherited classes and has an inherited class, interface can be instantiated using the defined method.

Reflection is to delay the interface Instantiation to the running stage. Therefore, reflection is generally used together with interfaces.

 

There are many scenarios, and I think many of them are used in the abstract factory mode. A typical example is the data read layer.
A project may use SqlSever, Access, Orace, Txt, and XML to Access data. Their methods are uniform, such as adding, deleting, repairing, and reading data.

This is to define an IDataAccess interface, which defines a unified method, add, delete, repair, read, and so on, and then inherit this interface with different implementation classes,

For example, SqlServer and XmL are defined as SqlServerDataAccess and XMLDataAccess. They all inherit from IDataAccess.

During application, the project can use Sqlserver or XML database through simple modification or configuration. At this time, reflection can be used to determine which implementation class the IDataAccess interface uses.

In the abstract factory mode, use the configuration file to set whether to use Sqlserver or XML data Implementation classes ., In this way, you can modify the configuration file to decide to use

Sqlserver or XML data Implementation class

Public IDataAccess CreateDatAccess ()

{

IDataAccess IDA = (IDataAccess) Assembly. Load ("Configure node Assembly"). CreateInstance ("namespace. Sqlserver ");

// IDataAccess IDA = (IDataAccess) Assembly. Load ("Configure node Assembly"). CreateInstance ("namespace. XML ");

Return IDA;

}

This example can be completely solved using the solution of the first scenario, but the implementation is complicated due to the many interfaces.

This example describes multiple interfaces. Each interface may have one implementation class or multiple implementation classes.

Project Structure

 

Define two interfaces in leleapplication2.framework

    

Define the Product interface implementation class in leleapplication2.impl. Product.

    

Define the ICar implementation class in ConsoleApplication2.Impl. Car.

   {
Console.WriteLine( }
}
{
Console.WriteLine( }
}

 

Then start the key code section

1. Establish the correspondence between interfaces and implementation classes and save them to the dictionary set.

Public {
S = (file Directory. GetFiles (s, {ass = // get the Assembly dll Console. WriteLine (ass. FullName );
(Type type ass. GetTypes (). Where (p => {Console. WriteLine (type. FullName); Type [] interfaces = (Type inter {
(! {Dictionary. Add (inter, List <Type>}
Dictionary [inter]. Add (type );
}
}
}
}

When reading implementation classes based on interfaces, you can use generics to implement different interfaces.

 
// The specifiedImplType parameter can be blank. If an interface has multiple implementation classes, you must specify which implementation class to use.
T GetImpTypeByInterface <T> (specifiedImplType =) where T: class Type interfaceType = (dictionary. Count >&{ Type implType = (specifiedImplType = {
ImplType =}
ImplType = dictionary [interfaceType]. Where (p => p. Name =}
Activator. CreateInstance (implType )}
Exception (

Test Run

= GetImpTypeByInterface<ICar>();  ICar iCar = GetImpTypeByInterface<ICar>(“QQCar”); iCar.Run();

 

In this way, you can find the corresponding implementation Class Based on the Interface Class,

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.