[总结]如何利用反射得到中间层中的所有类(利用XPO来做的项目中的例子)

本人做的一个利用XPO来做的项目中的例子

1.利用反射得到中间层中的所有类生成所有表
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.利用反射得到中间层中的所有类生成所有表(除编码表)

    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.删除所有表的数据(前提是表之间无联系)
    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



下载个reflector自己看

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

http://community.csdn.net/Expert/topic/4318/4318117.xml?temp=9.945315E-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



假设另一个工程中的所有类都编译到一个dll文件中了,在这很多的类当中,有一个类叫StringUtil,名称空间在HSMP.CommonBasic.Common下
该类中有一个方法:
public double GetSum(double x,double y)
{
return x+y;
}
编译后dll文件的存放路径是:D:\Test\HSMP.CommonBasic.dll
现在的问题是,如何通过程序调用该dll文件中的GetSum方法
大概有以下几步:
using System.Reflection;
A.
//这里要用LoadFrom,只有在本工程里添加了该dll的引用后才可以使用Load
Assembly objAss = Assembly.LoadFrom(@"D:\Test\HSMP.CommonBasic.dll");
//HSMP.CommonBasic.Common.StringUtil类的全路径
Type t=objAss.GetType("HSMP.CommonBasic.Common.StringUtil");
//动态生成类StringUtil的实例
object obj=System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个int参数,如果方法没有参数,就声明一个长度为0的数组
System.Type[] paramTypes = new System.Type[2];
paramTypes[0] = System.Type.GetType("System.Int32");
paramTypes[1] = System.Type.GetType("System.Int32");
//找到对应的方法
MethodInfo p = t.GetMethod("SayHello", paramTypes)
//参数值,如果所调用的方法没有参数,不用写这些
Object[] parameters = new Object[2];
parameters[0] = 3;
parameters[1] = 4;
object objRetval = p.Invoke(obj, parameters); //如果没有参数,写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);//类的命名空间
Response.Write(t.Name);//类的名称
}

posted @ 2005-11-10 16:20  PointNet  阅读(1330)  评论(0编辑  收藏  举报