code_junkie

How do I get the subclass Type object in a static superclass function in .net, using reflection

Nov 13th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. public abstract class RepositoryBase
  2. {
  3. public static IList<RepositoryBase> Search()
  4. {
  5. //get all public properties of the inheriting subclass
  6. // I already have the rest of the search code
  7. }
  8. }
  9.  
  10. public class Base
  11. {
  12. static void Foo() {}
  13. }
  14.  
  15. public class Derived : Base {}
  16.  
  17. class Test
  18. {
  19. static void Main()
  20. {
  21. Derived.Foo();
  22. }
  23. }
  24.  
  25. public class SqlRepository : IRepository {
  26.  
  27. DB _db;
  28.  
  29. public SqlRepository(DB dataContext) {
  30. //override the current context
  31. //with the one passed in
  32. _db = dataContext;
  33.  
  34. }
  35.  
  36. public IQueryable<RepositoryBase> Search() {
  37.  
  38. ...
  39.  
  40. Dim CustList As List(Of CUSTOMER) = CUSTOMER.SELEC("Name = 'Peter'", "Name DESC")
  41. Dim myOrders As List(Of ORDER) = CustList(0).Orders
  42. CustList(0).Name = "John"
  43.  
  44.  
  45. Public Interface ITables 'used solely to unify all the table types
  46. ' ReadOnly Property PrimaryKey() As String is better to shadow it from SuperClass TableType
  47. End Interface
  48.  
  49. Public Class TableType(Of T As ITables)'this T is what you are looking for
  50. Public PrimaryKey As String
  51. Public Shared Function SELEC(Optional ByVal sWhere As String = "", Optional ByVal sOrderBy As String = "") As List(Of T)
  52. 'shared/static method to fill and return a typed List with the DB rows
  53. 'can be called using for example Type.SELEC()
  54. Dim oConn As New OdbcConnection(My.Settings.ConnectionString)
  55. Dim oComm As OdbcCommand = oConn.CreateCommand
  56. oComm.CommandText = "SELECT * FROM " & GetType(T).Name
  57. If sWhere.Length > 0 Then oComm.CommandText &= " WHERE " & sWhere : If sOrderBy.Length > 0 Then oComm.CommandText &= " ORDER BY " & sOrderBy
  58. Dim oListRet As New List(Of T)
  59. oConn.Open()
  60. Dim oDR As OdbcDataReader = oComm.ExecuteReader
  61. Dim oneRow As T
  62. Do While oDR.Read
  63. oneRow = Activator.CreateInstance(GetType(T))
  64. For i = 0 To oDR.FieldCount - 1
  65. Dim value = oDR.Item(i)
  66. If TypeOf value Is DBNull Then value = Activator.CreateInstance(oDR.GetFieldType(i)) ' default value
  67. oneRow.GetType.GetProperty(oDR.GetName(i)).SetValue(oneRow, value, Nothing)
  68. Next
  69. oListRet.Add(oneRow)
  70. Loop
  71. oDR.Close()
  72. oConn.Close()
  73. oConn.Dispose()
  74. Return oListRet
  75. End Function
  76.  
  77. Public Function UPDATE(Optional ByVal sWhere As String = "") As Integer
  78. 'not shared but one for all tables
  79. 'working on this, almost finished
  80. End Function
  81. Shared Function fnPropAttribute(ByVal oProp As PropertyInfo, ByVal sAttrName As String) As String
  82. 'working on this. Returns for example the value of the attribute 'Category' of a field
  83. Dim attributes As AttributeCollection = TypeDescriptor.GetProperties(oProp.DeclaringType)(oProp.Name).Attributes
  84. Dim myAttribute As CategoryAttribute = CType(attributes(GetType(need to know wth to put here)), CategoryAttribute)
  85. Return myAttribute.Category
  86. End Function
  87. End Class 'TableType
  88.  
  89.  
  90. Public Class Tables
  91. Public Class CUSTOMER
  92. Inherits TableType(Of CUSTOMER)
  93. Implements ITables
  94. Public Shadows Const PrimaryKey As String = "idCust"
  95.  
  96. 'this returns the List(Of Orders) with my idCust
  97. Public ReadOnly Property ORDERs() As List(Of ORDER)
  98. Get
  99. Return ORDER.SELEC("idCust = " & Me.idCust)
  100. End Get
  101. End Property
  102.  
  103. Dim _idCust As Integer
  104.  
  105. 'this field's Attributes will be used in UPDATE, INSERT, etc
  106. 'Category 'Columns' is to distingish between DB fields and other possible properties
  107. 'Description is the field's ODBCType
  108. <Category("Columns"), Description("CHAR")> _
  109. Public Property idCust() As Integer
  110. Get
  111. Return _idCust
  112. End Get
  113. Set(ByVal value As Integer)
  114. _idCust = value
  115. End Set
  116. End Property
  117.  
  118. Dim _Name As String
  119. <Category("Columns"), Description("CHAR")> _
  120. Public Property Name() As String
  121. Get
  122. Return _Name
  123. End Get
  124. Set(ByVal value As String)
  125. _Name = value
  126. End Set
  127. End Property
  128. 'etc...
  129. End Class 'Customer
  130. Public Class ORDER
  131. Inherits TableType(Of ORDER)
  132. Implements ITables
  133. Public Shadows Const PrimaryKey As String = "idOrder"
  134.  
  135. Dim _idOrder As Integer
  136. <Category("Columns"), Description("CHAR")> _
  137. Public Property idOrder() As Integer
  138. Get
  139. Return _idOrder
  140. End Get
  141. Set(ByVal value As Integer)
  142. _idOrder = value
  143. End Set
  144. End Property
  145.  
  146. Dim _idCust As Integer
  147. <Category("Columns"), Description("CHAR")> _
  148. Public Property idCust() As Integer
  149. Get
  150. Return _idCust
  151. End Get
  152. Set(ByVal value As Integer)
  153. _idCust = value
  154. End Set
  155. End Property
  156.  
  157. Dim _Artic As String
  158. <Category("Columns"), Description("CHAR")> _
  159. Public Property Artic() As String
  160. Get
  161. Return _Artic
  162. End Get
  163. Set(ByVal value As String)
  164. _Artic = value
  165. End Set
  166. End Property
  167. 'etc...
  168. End Class 'Order
  169. End Class 'Tables
Add Comment
Please, Sign In to add comment