【问题标题】:Iterate through class properties遍历类属性
【发布时间】:2012-10-29 20:21:48
【问题描述】:

我有一个名为 ticket 的 VB.NET 类,它有几个“字段”类型的公共属性。我想有一种方法可以遍历所有这些属性(每个属性都有一个)并在每个属性上执行特定任务。我认为也许最好的方法是创建一个列表(字段)并用该类的“字段”属性填充列表。我不知道如何动态地将属性添加到列表中,这样如果我将来添加属性,我就不必手动将它们输入到列表中。关于我如何做到这一点的任何想法?我尝试搜索并找到了一些使用反射的示例,但我只能弄清楚如何获取属性的名称而不是属性本身。

这是一个类的例子:

Public Class ticket
    Public Property location As New field
    Public Property user As New field
    Public Property callType As New field
    Public Property dateOfCall As New field
    Public Property tech As New field
    Public Property description As New field

    Public Property myFields As New List(Of field)

'What if field had a property of value and I wanted to increment all of the fields    in this class by one

Public Sub plusOne()
    For Each x As field In myFields()
        x.value += 1
    Next
End Sub

End Class

【问题讨论】:

    标签: vb.net class reflection properties


    【解决方案1】:

    您想使用Reflection,这意味着检查程序集中的类型。您可以通过 System.Reflection 命名空间来执行此操作。

    有关 VB.Net 中反射的示例,请参阅 msdn 杂志上的以下文章: http://msdn.microsoft.com/en-us/magazine/cc163750.aspx

    该文章中迭代类型成员的示例如下:

    Dim t As Type = GetType(AcmeCorp.BusinessLogic.Customer)
    For Each member As MemberInfo In t.GetMembers
      Console.WriteLine(member.Name)
    Next
    

    【讨论】:

    • 如何实现类似 names.add(member) 的功能?还是我以错误的方式接近这个?
    【解决方案2】:

    再次作为之前的答案 - 您将使用反射。以List(Of T) 上的Add 为例,我会这样做。

    Public Class Test
      Public Property SomeList As List(Of String)
    End Class
    

    然后使用下面的代码在List(Of String)上调用add

    Dim pi As PropertyInfo = GetType(Test).GetProperty("SomeList")
    Dim mi As MethodInfo = GetType(List(Of String)).GetMethod("Add")
    Dim t As New Test()
    t.SomeList = New List(Of String)
    mi.Invoke(pi.GetValue(t, Nothing), New Object() {"Added through reflection"})
    

    【讨论】:

      【解决方案3】:

      正如前面的答案所说,您需要使用 System.Reflection 来获取类的属性。然后检查属性是你想要的类型。

      希望这能满足您的需求。如果您运行代码,您将看到它只采用指定类型的属性。如果您想拥有所有属性,请删除 for each 循环上的 where 语句。

      Imports System.Reflection
      
      Module Module1
      
      Sub Main()
      
          ' Create a list to hold your properties
          Dim myList As New List(Of MyProperty)
      
          ' check each property for its type using the where statement below. Change integer to "Field" in your case
          For Each el In GetType(Test).GetProperties.Where(Function(p) p.PropertyType = GetType(Integer))
              ' add each matching property to the list
              myList.Add(New MyProperty With {.Name = el.Name, .GetMethod = el.GetGetMethod(), .SetMethod = el.GetSetMethod()})
              Console.WriteLine(el.Name & " has been added to myList")
          Next
      
          Console.Read()
      End Sub
      
      Public Class MyProperty
          Public Property Name As String
          Public Property GetMethod As MethodInfo
          Public Property SetMethod As MethodInfo
      End Class
      
      Public Class Test
          Private var1 As String
          Private var2 As String
          Private var3 As String
          Private var4 As String
      
          Public Property myInt1 As Integer
          Public Property myInt2 As Integer
          Public Property myInt3 As Integer
          Public Property myInt4 As Integer
      End Class
      End Module
      

      希望有帮助

      【讨论】:

      • 如果我错了,请纠正我,但这会创建一个重复的对象列表,它们不一样。因此,如果我为每个修改列表中的每个测试,我将不会修改该类实例的属性。
      • 完全不在我的脑海中,但它应该可以工作,因为该类保留了引用。您可以通过将上面的代码转储到控制台应用程序来尝试。我不能在 atm 上做,否则我会为你验证
      猜你喜欢
      • 2011-12-30
      • 2010-10-26
      • 1970-01-01
      • 2014-10-25
      • 2016-08-11
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多