缓存抽象类的属性信息

问题描述:

我一直在玩弄实现抽象基类,使用反射完成SQL到对象映射。缓存抽象类的属性信息

我做了一些基准测试,并决定要实现对象的属性信息的缓存策略(以防止将来查找它们)。我的第一本能是尝试和实现这样的事情。

Public MustInherit Class BaseModel 
    Implements IFillable 

    Private Shared PropertyCache As List(Of PropertyInfo) 

    Sub New() 
     PropertyCache = New List(Of PropertyInfo) 
     For Each itm As PropertyInfo In Me.GetType().GetProperties 
      PropertyCache.Add(itm) 
     Next 
    End Sub 
End Class 

但后来我意识到这显然不起作用,因为它会在随后的对象实例化上被覆盖。

所以,现在我坚持下去,你如何实现一个缓存其反射“元数据”的抽象类?

编辑:

这是最好的(解决方法我的问题),我可以拿出而言,我希望有人能提出更好的东西?

Public MustInherit Class BaseModel 
    Implements IFillable 

    Private Shared ReadOnly PropertyCache As New Dictionary(Of String, PropertyInfo) 

    Sub New() 
     Dim typeName As String = Me.GetType.ToString 
     For Each itm As PropertyInfo In Me.GetType().GetProperties 
      Dim lookupKey As String = String.Format("{0}_{1}", typeName, itm.Name) 
      If Not PropertyCache.ContainsKey(lookupKey) Then 
       PropertyCache.Add(lookupKey, itm) 
      End If 
     Next 
    End Sub 
End Class 

如果将代码置于Shared Sub New中,则应该只执行一次。

http://msdn.microsoft.com/en-us/library/aa711965(v=vs.71).aspx