Use reflection technology to dynamically create class objects (instance code)

Source: Internet
Author: User

After several days of research, the data access layer can be dynamically loaded. Although there are many reflections on the InternetArticleIn theory, however, there is no detailed example, so I have never been able to write the actualCode. Write a piece of code here, hoping to help beginners like me get started quickly, apply the Code first, and then go deep, so as not to worry.

First, let's talk about the basic information. In this exampleSort(Category) based on access. The table structure will not be mentioned, and I do not want to involve specific operations on the database, mainly to look at the ideas. For some concepts and basic knowledge about reflection, please refer to relevant articles. I have written very well on the Internet, so I will not be able to make a wrong shift.

First, create several directories for storing class files:

IdalIndicates the data access layer interface;

SqlserverdalIndicates the data access layer for operations.SQL ServerDatabase;

AccessdalIndicates the data access layer for operations.AccessDatabase;

DalfactoryIndicates the class factory of the data access layer class;

BllIndicates the business logic layer;

Create a class under each folder. The code of the corresponding class is as follows. The namespace is"Koalastudio. bookshopportal", Only the most necessary code is retained here.

(1First, create a data access layer interface.Koalastudio. bookshopportal. idal. isort:

UsingSystem;

UsingSystem. Data;

NamespaceKoalastudio. bookshopportal. idal

{

Interface Isort

{

DatasetGetlist ();

}

}

Note: a data access layer interface is defined first. All data access layer classes must be derived from this interface, so that unified calling methods can be provided for the business logic layer.

(2) And then createData access layerKoalastudio. bookshopportal. sqlserverdal. SortAndKoalastudio. bookshopportal. accessdal. Sort

UsingSystem;

UsingSystem. Data;

UsingKoalastudio. bookshopportal. idal;

NamespaceKoalastudio. bookshopportal. sqlserverdal

{

Class Sort:Isort{

// Obtain the category information list. 

Public DatasetGetlist ()

{

//Here isSQLDatabase access code. The result is returned as a dataset.

}

}

}

NamespaceKoalastudio. bookshopportal. accessdal

Class Sort:Isort 

{

// Obtain the category information list. 

Public DatasetGetlist ()

{

//Here isAccessDatabase access code. The result is returned as a dataset. }

}

}

Note: Both classes are derived fromIsortThe same functions are implemented. The difference is that the database to be operated is different.

(3) Below is the implementation of the class factory

UsingSystem;

UsingSystem. reflection;

UsingKoalastudio. bookshopportal. idal;

NamespaceKoalastudio. bookshopportal. dalfactory

{

Class Sort

{

Public StaticKoalastudio. bookshopportal. idal.IsortCreate ()

{

//Background useSQL ServerDatabase

StringClassname ="Koalastudio. bookshopportal. sqlserverdal. Sort";

//If you useAccessDatabase, writeTo:

// String classname = "koalastudio. bookshopportal. accessdal. Sort ";

 TypeT =Type. GetType (classname );

Return(Isort)Activator. Createinstance (t );

}

}

}

Note: first obtain a string that is the name of the data access layer class to be used. In the actual environment, you need to store this string in the configuration file.ProgramDuring the runtime, the code for operating the database is modified as long as the string is modified, instead of re-compiling.

"TypeT =Type. GetType (classname );"Return the type information of the class based on the class name provided by the string, and then use"Activator. Createinstance (t)This class can be instantiated. Finally, use"(Isort)"Convert the obtained instance to"Koalastudio. bookshopportal. idal.Isort"Type.

(4) FinallyKoalastudio. bookshopportal. BLL. sorClass

UsingSystem;

UsingSystem. Data;

UsingKoalastudio. bookshopportal. idal;

NamespaceKoalastudio. bookshopportal. BLL

{

Public Class Sort

{

/// Back to category name list

Public Static DatasetGetnamelist ()

{

IsortSortdal = dalfactory.Sort. Create ();

ReturnSortdal. getmainlist ();

}

}

}

Description: Use " isort sortdal = dalfactory. sort . create (); " can directly generate an instance of the Data Sequence class. Here, we don't have to worry about what database is in the background. We just need to directly use the data metadata instance provided by the class factory.

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.