【问题标题】:VB.Net Search namespace for a generic type (Reflection)VB.Net 搜索泛型类型的命名空间(反射)
【发布时间】:2010-09-27 01:01:54
【问题描述】:

我正在尝试使用上下文动态注册实体和配置(仅限 ef4 代码)

我通常会这样做:

Private Shared Sub ConfigureDatabase(ByRef Builder As ContextBuilder(Of ContextExtension))
  'Load configurations for each of the tables (Entity sets) in our database...
  ConfigureEntity(Builder, New ContactConfig)
End Sub

 Private Shared Sub ConfigureEntity(Of T)(ByRef Builder As ContextBuilder(Of ContextExtension), ByVal config As EntityConfiguration(Of T), ByVal setName As String)
    'Register the entity configuration with the builder
    Builder.Configurations.Add(config)

    'Register the entity set name with the builder
    Builder.RegisterSet(Of T)(setName)
End Sub

其中ContactConfig 是继承EntityConfiguration(Of Contact) 的类,Contact 是实现接口IEntity 的类(IEntity 是所有实体通用的)。

所以...我需要在命名空间(比如 Project.DB.Client)中搜索所有匹配的签名:

EntityConfiguration(Of <Class which implements IEntity>)

我怎样才能做到这一点?

【问题讨论】:

    标签: .net vb.net entity-framework reflection


    【解决方案1】:

    命名空间只是对象名称的一部分。搜索继承泛型类的类型比仅按命名空间名称过滤要付出更多的努力:

    Private Function CheckType(ByVal type As Type) As Boolean
        If (type Is GetType(Object)) Then ' object hierarhy root reached'
            Return False
        End If
        If (type.IsGenericType) Then
            ' inherits from some generic type'
            Dim parameters = type.GetGenericArguments()
            ' check if it has generic 1 parameter'
            If parameters.Length = 1 Then
                ' check if generic parameter is defined'
                If Not (parameters(0).FullName Is Nothing) Then
                    ' check if parameter implements IEntity'
                    If GetType(IEntity).IsAssignableFrom(parameters(0)) Then
                        ' check if it is EntityConfiguration(Of T)'
                        If type Is GetType(EntityConfiguration(Of )).MakeGenericType(parameters) Then
                            Return True
                        End If
                    End If
                End If
            End If
        End If
        Return CheckType(type.BaseType)
    End Function
    
    Function FilterTypes(ByVal nameSpaceName As String, ByVal types As IEnumerable(Of Type)) As List(Of Type)
        Dim result As New List(Of Type)
        ' append . to namespace name'
        nameSpaceName = nameSpaceName + "."
        For Each type As Type In types
            ' we do not need abstract classes, wont be able to create their instances anyway'
            If (type.IsClass And Not type.IsAbstract And
                 type.FullName.StartsWith(nameSpaceName)) Then
                ' check if type is inherited from EntityConfiguration(Of T)'
                If (CheckType(type)) Then
                    result.Add(type)
                End If
            End If
        Next
        Return result
    End Function
    

    CheckType 函数检查类型是否继承自 EntityConfiguration(Of T)

    FilterTypes 函数还通过命名空间过滤类型并返回过滤列表结果。

    例如:Dim types = FilterTypes("Project.DB.Client", Assembly.GetExecutingAssembly().GetTypes())

    【讨论】:

    • 谢谢 - 我会尝试并回复你。
    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 2022-12-31
    • 2016-05-10
    • 2013-12-22
    相关资源
    最近更新 更多