[Conclusion] How to Use reflection to get all classes in the middle layer (examples of projects using xpo)

Source: Internet
Author: User

Here is an example of a project using xpo.

1. Use reflection to get all the classes in the middle layer to generate all the tables
Public shared function Init ()
Dim D as [Assembly] = [Assembly]. Load ("Dal ")
Dim types as type () = D. gettypes ()
Dim t as type

Devexpress. xpo. session. defaultsession. connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ httpcontext. current. server. mappath ("/") + "workdb \ familyservice. MDB ;"
Devexpress. xpo. session. defaultsession. autocreateoption = autocreateoption. schemaonly
For each t in Types
Dim OBJ as new object
If T. fullname <> "Dal. initdatabase" then
OBJ = D. createinstance (T. fullname)
'Obj = activator. createinstance (t)
OBJ. Save ()
End if

Next
End Function

2. Use reflection to get all the classes in the middle layer to generate all the tables (except the encoding table)

Public shared function initnocode ()
Dim D as [Assembly] = [Assembly]. Load ("Dal ")
Dim types as type () = D. gettypes ()
Dim t as type

'Devexpress. xpo. session. defaultsession. connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ httpcontext. current. server. mappath ("/") + "workdb \ familyservice. MDB ;"
'Devexpress. xpo. session. defaultsession. autocreateoption = autocreateoption. schemaonly
'Dim session as new devexpress. xpo. Session
'Session. connectionstring = devexpress. xpo. session. defaultsession. connectionstring
'Session. autocreateoption = autocreateoption. schemaonly

For each t in Types
Dim OBJ as new object

If T. fullname. substring (4, 1) <> "C" andalso T. fullname <> "Dal. initdatabase" then
OBJ = D. createinstance (T. fullname)
'Obj = activator. createinstance (t)
OBJ. Save ()
End if

next
end function
Public shared function initnocode2 ()
dim D as [Assembly] = [Assembly]. load ("Dal")
dim types as type () = D. gettypes ()
dim t as type

'Devexpress. xpo. session. defaultsession. connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ httpcontext. current. server. mappath ("/") + "workdb \ familyservice. MDB ;"
'Devexpress. xpo. session. defaultsession. autocreateoption = autocreateoption. schemaonly
'Dim session as new devexpress. xpo. Session
'Session. connectionstring = devexpress. xpo. session. defaultsession. connectionstring
'Session. autocreateoption = autocreateoption. schemaonly

For each t in Types
Dim OBJ as new object
If T. fullname = "Dal. c_occupation" then
OBJ = D. createinstance (T. fullname)
OBJ. Save ()
Else
If T. fullname. substring (4, 1) <> "C" andalso T. fullname <> "Dal. initdatabase" then
OBJ = D. createinstance (T. fullname)
'Obj = activator. createinstance (t)
OBJ. Save ()
End if
End if
Next
End Function

3. delete data from all tables (if there is no association between tables)
Public shared function deldata ()
dim D as [Assembly] = [Assembly]. load ("Dal")
dim types as type () = D. gettypes ()
dim t as type
dim tbname as string
dim command as idbcommand
command = devexpress. xpo. session. defaultsession. connectionprovider. createcommand ()

For each t in Types
Dim OBJ as new object
If T. fullname. substring (4, 1) <> "C" andalso T. fullname <> "Dal. initdatabase" then
Tbname = T. fullname. substring (4)
Command. commandtext = "delete from" + tbname
Command. executenonquery ()
End if

Next
End Function

Download a reflector.

Http://www.aisto.com/roeder/dotnet/

Http://community.csdn.net/Expert/topic/4318/4318117.xml? Temp = 9.942515e-02
Http://community.csdn.net/Expert/topic/4345/4345542.xml? Temp =. 5111048.

Http://search.microsoft.com/search/results.aspx? View = ZH-CN & St = A & NA = 81 & Qu = % E5 % 8f % 8d % E5 % B0 % 84

Assume that all the classes in another project are compiled into a DLL file. Among these classes, there is a class called stringutil, And the namespace is under HSMP. commonbasic. Common.
This class has a method:
Public double getsum (Double X, Double Y)
{
Return X + Y;
}
After compilation, the DLL file is stored in the path D: \ test \ HSMP. commonbasic. dll.
The question now is how to pass Program Call the getsum method in the DLL file.
You can take the following steps:
Using system. reflection;
A.
// Loadfrom is used here. load can be used only after the DLL reference is added in this project.
Assembly objass = assembly. loadfrom (@ "D: \ test \ HSMP. commonbasic. dll ");
// Full path of HSMP. commonbasic. Common. stringutil class
Type T = objass. GetType ("HSMP. commonbasic. Common. stringutil ");
// Dynamically generate stringutil-like instances
Object OBJ = system. activator. createinstance (t );
// Parameter information. getsum requires two int parameters. If the method does not have any parameters, it declares an array with a length of 0.
System. Type [] paramtypes = new system. Type [2];
Paramtypes [0] = system. type. GetType ("system. int32 ");
Paramtypes [1] = system. type. GetType ("system. int32 ");
// Find the corresponding method
Methodinfo P = T. getmethod ("sayhello", paramtypes)
// Parameter value. If the called method has no parameters, do not write these
Object [] parameters = new object [2];
Parameters [0] = 3;
Parameters [1] = 4;
Object objretval = P. Invoke (OBJ, parameters); // if no parameter exists, enter null.

----------------------------------------------------------------

Using system. reflection;

Assembly A = assembly. loadfrom (@ "D: \ marykay \ commonbasic \ bin \ marykay. commonbasic. dll ");
Foreach (type t in a. gettypes ())
{
Response. Write (T. namespace); // namespace of the class
Response. Write (T. Name); // Class Name
}

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.