vb.net 遍历类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13309353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate through class properties
提问by donL
I have a VB.NET class called ticket that has several public properties of type 'field'. I would like to have a way that I can iterate through all of these properties (with a for each) and perform a specific task on each of them. I thought that maybe the best way was to create a list(Of field) and fill the list with the 'field' properties of that class. What I don't know how to do is get the properties into the list dynamically so that if I add properties in the future I don't have to type them into the list manually. Any thoughts on how I might do this? I tried searching for and found some examples of using reflection but I could only figure out how to get at the name of the property and not the property itself.
我有一个名为 ticket 的 VB.NET 类,它具有多个“字段”类型的公共属性。我想有一种方法可以遍历所有这些属性(每个属性都有一个)并对每个属性执行特定任务。我认为也许最好的方法是创建一个列表(字段)并用该类的“字段”属性填充列表。我不知道如何做的是将属性动态添加到列表中,这样如果我将来添加属性,我就不必手动将它们键入到列表中。关于我如何做到这一点的任何想法?我尝试搜索并找到了一些使用反射的示例,但我只能弄清楚如何获取属性的名称,而不是属性本身。
Here is an example of a class:
下面是一个类的例子:
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
回答by Seth Flowers
You want to use Reflection, which just means inspecting the types in an assembly. You can do this via the System.Reflectionnamespace.
您想使用Reflection,这意味着检查程序集中的类型。您可以通过System.Reflection命名空间执行此操作。
See the following article on the msdn magazine for examples of reflection in VB.Net: http://msdn.microsoft.com/en-us/magazine/cc163750.aspx
有关 VB.Net 中的反射示例,请参阅 msdn 杂志上的以下文章:http: //msdn.microsoft.com/en-us/magazine/cc163750.aspx
An example of iterating over the members of a type in that article is as follows:
迭代该文章中类型的成员的示例如下:
Dim t As Type = GetType(AcmeCorp.BusinessLogic.Customer)
For Each member As MemberInfo In t.GetMembers
Console.WriteLine(member.Name)
Next
回答by LukeHennerley
Again as the previous answer - you would use reflection. To call as an example Addon a List(Of T)I would do this.
再次作为上一个答案-您将使用反射。以 a 为例Add,List(Of T)我会这样做。
Public Class Test
Public Property SomeList As List(Of String)
End Class
Then use the following code to call add on a List(Of String)
然后使用以下代码调用 add on a List(Of String)
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"})
回答by Stuart Blackler
As the previous answers have said, you need to use System.Reflection to get the properties of the class. Then check the property is the type that you want.
正如前面的答案所说,您需要使用 System.Reflection 来获取类的属性。然后检查属性是您想要的类型。
Hopefully this should give you what you want. If you run the code you will see that it only takes the properties of the specified type. If you want to have all properties, remove the where statement on the for each loop.
希望这应该给你你想要的。如果您运行代码,您将看到它只采用指定类型的属性。如果您想拥有所有属性,请删除 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
Hope that helps
希望有帮助

