好了,所以我想做出能够产生TSQL查询来搜索所有子类的公共字符串属性的数据访问对象一个不错的超类。我想使用反射来获取子类的类型,然后通过所有对象的公共字符串属性的迭代,因为这些属性名称是相同的数据库列名。然后,我可以使用这些属性名称以生成TSQL查询。

[警告:哎,ID而使用的nhibernate但我不可能说服这些人使用该]

[我也可以用泛型解决这个问题,但我认为他们发现泛型可怕,因为他们是VB.net人(对不起,如果我伤害了你的感情VB.net一瞥;()]

确定这样的基础对象是这样的:

public abstract class RepositoryBase
{
   public static IList<RepositoryBase> Search()
   {
        //get all public properties of the inheriting subclass
        // I already have the rest of the search code
   }
}

这甚至可能,或可取?

虽然我打字这一点,我很喜欢“拧,我就用泛型做到这一点。”

感谢阅读!

有帮助吗?

解决方案

当调用“通过”派生类的静态方法,该编译器将解析所以IL实际上包含基类中的方法。例如:

public class Base
{
    static void Foo() {}
}

public class Derived : Base {}

class Test
{
    static void Main()
    {
        Derived.Foo();
    }
}

在主呼叫将实际上最终(从C#编译时为至少)编译为Base.Foo()在IL。所以你不能在执行时告诉一下原来的电话是。

这听起来像泛型是要走的途径。

其他提示

我想大多数库模式使用接口而不是抽象类是这样的...

 public class SqlRepository : IRepository {

        DB _db;

        public SqlRepository(DB dataContext) {
            //override the current context
            //with the one passed in
            _db = dataContext;

        } 

        public IQueryable<RepositoryBase> Search() {

           ...

我不认为我所见过写这样的模式。我想这是可能的,但我不认为你将能够实现你的想法yuo're试图完成的任务。检查出来...的 http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/19a968ce-7d8b-4cf9-b0f0-292464f1e708/

是否有任何理由,你不能使用接口呢?

好吧,假设你还是要一个答案:没有。在静态方法你不是在一个对象的上下文。你不知道谁一直叫你(没有构建StackTracede),即使你做到了,你不知道来电者是为谁属性应该被列举的对象。

如果它是一个实例方法,你可以只调用GetType()

我刚工作(VB)在你提出什么。 此代码的工作(译自西班牙语因此它可能包含错别字): 例如,它可用于这种方式:

Public Sub Main()
   'the SELEC method used below this line is written once and called from every table/class'
   Dim CustList As List(Of CUSTOMER) = CUSTOMER.SELEC("Name = 'Peter'", "Name DESC")
   Dim myOrders As List(Of ORDER) = CustList(0).Orders
   CustList(0).Name = "John"
End Sub


Public Interface ITables 'used solely to unify all the table types'
    '    ReadOnly Property PrimaryKey() As String is better to shadow it from SuperClass TableType'
End Interface

Public Class TableType(Of T As ITables)'this T is what you are looking for'
    Public PrimaryKey As String
    Public Shared Function SELEC(Optional ByVal sWhere As String = "", Optional ByVal sOrderBy As String = "") As List(Of T)
        'shared/static method to fill and return a typed List with the DB rows'
        'can be called using for example Type.SELEC()'
        Dim oConn As New OdbcConnection(My.Settings.ConnectionString)
        Dim oComm As OdbcCommand = oConn.CreateCommand
        oComm.CommandText = "SELECT * FROM " & GetType(T).Name
        If sWhere.Length > 0 Then oComm.CommandText &= " WHERE " & sWhere
        If sOrderBy.Length > 0 Then oComm.CommandText &= " ORDER BY " & sOrderBy
        Dim oListRet As New List(Of T)
        oConn.Open()
        Dim oDR As OdbcDataReader = oComm.ExecuteReader
        Dim oneRow As T
        Do While oDR.Read
            oneRow = Activator.CreateInstance(GetType(T))
            For i = 0 To oDR.FieldCount - 1
                Dim value = oDR.Item(i)
                If TypeOf value Is DBNull Then value = Activator.CreateInstance(oDR.GetFieldType(i)) ' default value'
                oneRow.GetType.GetProperty(oDR.GetName(i)).SetValue(oneRow, value, Nothing)
            Next
            oListRet.Add(oneRow)
        Loop
        oDR.Close()
        oConn.Close()
        oConn.Dispose()
        Return oListRet
    End Function

    Public Function UPDATE(Optional ByVal sWhere As String = "") As Integer
        'not shared but one for all tables'
        'working on this, almost finished'
    End Function
    Shared Function fnPropAttribute(ByVal oProp As PropertyInfo, ByVal sAttrName As String) As String
        'working on this. Returns for example the value of the attribute 'Category' of a field'
        Dim attributes As AttributeCollection = TypeDescriptor.GetProperties(oProp.DeclaringType)(oProp.Name).Attributes
        Dim myAttribute As CategoryAttribute = CType(attributes(GetType(need to know wth to put here)), CategoryAttribute)
        Return myAttribute.Category
    End Function
End Class 'TableType'


Public Class Tables
    Public Class CUSTOMER
        Inherits TableType(Of CUSTOMER)
        Implements ITables
        Public Shadows Const PrimaryKey As String = "idCust"

        'this returns the List(Of Orders) with my idCust'
        Public ReadOnly Property ORDERs() As List(Of ORDER)
            Get
                Return ORDER.SELEC("idCust = " & Me.idCust)
            End Get
        End Property

        Dim _idCust As Integer

        'this Attributes will be used in UPDATE, INSERT, etc'
        'Category 'Columns' is to distingish between DB fields and other possible properties'
        'Description is the ODBCType of the field'
        <Category("Columns"), Description("CHAR")> _
        Public Property idCust() As Integer
            Get
                Return _idCust
            End Get
            Set(ByVal value As Integer)
                _idCust = value
            End Set
        End Property

        Dim _Name As String
        <Category("Columns"), Description("CHAR")> _
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
        'etc...'
    End Class 'Customer'
    Public Class ORDER
        Inherits TableType(Of ORDER)
        Implements ITables
        Public Shadows Const PrimaryKey As String = "idOrder"

        Dim _idOrder As Integer
        <Category("Columns"), Description("CHAR")> _
        Public Property idOrder() As Integer
            Get
                Return _idOrder
            End Get
            Set(ByVal value As Integer)
                _idOrder = value
            End Set
        End Property

        Dim _idCust As Integer
        <Category("Columns"), Description("CHAR")> _
        Public Property idCust() As Integer
            Get
                Return _idCust
            End Get
            Set(ByVal value As Integer)
                _idCust = value
            End Set
        End Property

        Dim _Artic As String
        <Category("Columns"), Description("CHAR")> _
        Public Property Artic() As String
            Get
                Return _Artic
            End Get
            Set(ByVal value As String)
                _Artic = value
            End Set
        End Property
        'etc...'
    End Class 'Order'
End Class 'Tables'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top