Vb.net 如何将类作为参数传递,然后使用该类的共享方法?

Vb.net 如何将类作为参数传递,然后使用该类的共享方法?,vb.net,class,arguments,Vb.net,Class,Arguments,注意:这个问题不是问如何传递类的实例。它询问如何通过具有共享项的类 例如。。。 有没有关于如何制作一个函数来实现这一点的想法 Public Function InsertSQL(ByVal xClass As Type) As String InsertSQL = "Insert into " InsertSQL = InsertSQL & xClass.TableName InsertSQL = InsertSQL & " (" InsertSQ

注意:这个问题不是问如何传递类的实例。它询问如何通过具有共享项的类

例如。。。 有没有关于如何制作一个函数来实现这一点的想法

Public Function InsertSQL(ByVal xClass As Type) As String
    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & xClass.TableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & xClass.FieldList
    InsertSQL = InsertSQL & " )"
End Function
使用一组类似这样的类:

Public Class cClient
    Public Shared TableName = "Clients"
    Public Shared Function FieldList() As String
        FieldList = "Name, Phone"
    End Function
End Class

Public Class cSale
    Public Shared TableName = "Sales"
    Public Shared Function FieldList() As String
        FieldList = "Name, Amount"
    End Function
End Class
我在Google上搜索过“如何将类作为参数传递”,但没有找到任何可以访问类的共享方法的方法。我只能得到我想通过该类实例的结果

请注意,此代码只是一个示例。 当然,如果我通过一个类的实例,我没有问题。
但是-这不是我想弄明白的。

我会用在你的情况下。 像这样的

Public Sub Main
    Dim c = new cClient()
    Dim sql = InsertSQL(c)
    Console.WriteLine(sql)
End Sub


' Pass an actual instance of a class that inehrits cTableBase'
' all of these classes have the same BASE functions and property
' defined in the base class
Public Function InsertSQL(ByVal xClass As cTableBase) As String
    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & xClass.TableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & xClass.FieldList
    InsertSQL = InsertSQL & " )"
End Function

Public Class cTableBase
    Public TableName As String 
    Public Overridable Function FieldList() As String
    End Function
End Class

Public Class cClient 
    Inherits cTableBase
    Public Sub New
        TableName = "Clients"
    End Sub
    Public Overrides Function FieldList() As String
        FieldList = "Name, Phone"
    End Function
End Class

Public Class cSale 
    Inherits cTableBase
    Public Sub New
        TableName = "Sales"
    End Sub
    Public Overrides Function FieldList() As String
        FieldList = "Name, Amount"
    End Function
End Class
正如你在下面的评论中所说的那样。这可以通过使用反射传递给InsertSQL方法的类型来完成

Sub Main
    Dim c = new cClient()
    Dim result = InsertSQL(c.GetType())
    Console.WriteLine(result)
End Sub

Public Function InsertSQL(ByVal xClass As Type) As String

    Dim fi = xClass.GetField("TableName")
    Dim tableName = fi.GetValue(xClass)

    Dim mi = xClass.GetMethod("FieldList")
    Dim fieldList = mi.Invoke(xClass, Nothing)

    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & tableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & fieldList
    InsertSQL = InsertSQL & " )"
End Function

使用继承,无需共享成员当所有类都从具有公共功能的基类继承时在函数中声明字符串,对该字符串变量执行insert语句并返回该字符串。共享变量、属性、函数,子例程意味着可以在不创建类实例的情况下访问它们。这两个类中只有共享的内容,因此您不需要将该类的实例传递给InsertSql,就可以在函数中使用这些类。对于目前为止回答的所有问题:我已经声明:“如果我传递一个类的实例,我没有问题。但是-这不是我想要解决的问题。”我已经声明:,如果传递类的实例,则没有问题。但是,这不是我想要弄明白的。“让我尝试使用反射,然后使用反射添加不同的方法。更复杂一点,有趣一点。。。因此,您必须基本上挖掘出使用共享项的能力。请注意,您仍然需要一个实例来提取要传递给InsertSQL的类型。仍然没有找到直接传递cClient类的方法