【问题标题】:How do i pass a class as an argument and then use a shared method of that class?我如何将一个类作为参数传递,然后使用该类的共享方法?
【发布时间】:2014-06-28 14:58:33
【问题描述】:

注意:这个问题不是问如何传递一个类的 instance。它询问如何传递具有共享项目的 类。

例如... 关于如何制作一个可以做到这一点的函数的任何想法?

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 上搜索了“如何将类作为参数传递”,但没有找到任何可以访问类共享方法的方法。我只能得到认为我想要传递该类的实例的结果。

请注意,此代码只是一个示例。 而且 - 当然,如果我传递一个类的实例,我没有问题。
但是 - 这不是我想要弄清楚的。

【问题讨论】:

  • 使用继承,当你们都类时不需要共享成员从具有通用功能的基类继承
  • 在您的函数中声明一个字符串,在该字符串变量上创建插入语句并返回该字符串。
  • 共享变量、属性、函数和子例程意味着无需创建类的实例即可访问它们。这两个类只有共享的东西,所以你不需要将类的实例传递给 InsertSql 就可以使用函数内部的类。
  • 到目前为止所有回答的人:我已经说过:“如果我传递一个类的实例,我没有问题。但是 - 这不是我想要弄清楚的。”

标签: vb.net class arguments


【解决方案1】:

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

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

【讨论】:

  • 我已经说过:“如果我传递一个类的实例,我没有问题。但是 - 这不是我想要弄清楚的。”
  • 那么让我尝试一下使用反射
  • 添加了一种不同的反射方法。有点复杂
  • 有趣...所以你必须基本上挖掘出使用共享项目的能力。
  • 请注意,您仍然需要一个实例来提取要传递给 InsertSQL 的类型。还没找到直接传cClient类的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 2021-04-23
  • 2015-10-04
  • 2016-03-30
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多