如何遍历一个类的所有属性?

我有一堂课。

Public Class Foo Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Age As String Public Property Age() As String Get Return _Age End Get Set(ByVal value As String) _Age = value End Set End Property Private _ContactNumber As String Public Property ContactNumber() As String Get Return _ContactNumber End Get Set(ByVal value As String) _ContactNumber = value End Set End Property End Class 

我想循环通过上面的类的属性。 例如;

 Public Sub DisplayAll(ByVal Someobject As Foo) For Each _Property As something In Someobject.Properties Console.WriteLine(_Property.Name & "=" & _Property.value) Next End Sub 

使用reflection:

 Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null)); } 

编辑:你也可以指定一个BindingFlags值为type.GetProperties()

 BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; PropertyInfo[] properties = type.GetProperties(flags); 

这将限制返回的属性公共实例属性(不包括静态属性,受保护的属性等)。

你不需要指定BindingFlags.GetProperty ,你可以在调用type.InvokeMember()来获取属性的值时使用它。

请注意,如果您正在讨论的对象具有自定义属性模型(例如DataTable DataRowView等),则需要使用TypeDescriptor ; 好消息是这对于普通class级来说还是很好的(甚至可以比反思快得多 ):

 foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(obj)) { Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(obj)); } 

这也提供了对TypeConverter进行格式化的简单访问:

  string fmt = prop.Converter.ConvertToString(prop.GetValue(obj)); 

由Brannon给出的C#的VB版本:

 Public Sub DisplayAll(ByVal Someobject As Foo) Dim _type As Type = Someobject.GetType() Dim properties() As PropertyInfo = _type.GetProperties() 'line 3 For Each _property As PropertyInfo In properties Console.WriteLine("Name: " + _property.Name + ", Value: " + _property.GetValue(Someobject, Nothing)) Next End Sub 

使用绑定标志而不是第3行

  Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Instance Dim properties() As PropertyInfo = _type.GetProperties(flags) 

反思很“重”

也许试试这个解决scheme:// C#

 if (item is IEnumerable) { foreach (object o in item as IEnumerable) { //do function } } else { foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties()) { if (p.CanRead) { Console.WriteLine("{0}: {1}", p.Name, p.GetValue(obj, null)); //possible function } } } 

“VB.Net

  If TypeOf item Is IEnumerable Then For Each o As Object In TryCast(item, IEnumerable) 'Do Function Next Else For Each p As System.Reflection.PropertyInfo In obj.GetType().GetProperties() If p.CanRead Then Console.WriteLine("{0}: {1}", p.Name, p.GetValue(obj, Nothing)) 'possible function End If Next End If 

reflection减慢+/- 1000倍的方法调用的速度,显示在“日常事物的性能”中

 private void ResetAllProperties() { Type type = this.GetType(); PropertyInfo[] properties = (from c in type.GetProperties() where c.Name.StartsWith("Doc") select c).ToArray(); foreach (PropertyInfo item in properties) { if (item.PropertyType.FullName == "System.String") item.SetValue(this, "", null); } } 

我使用上面的代码块来重置我的web用户控件对象中的所有string属性,这些名称以“Doc”开头。

下面是使用LINQ lambda的另一种方法:

C#:

 SomeObject.GetType().GetProperties().ToList().ForEach(x => Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, null)}")); 

VB.NET:

 SomeObject.GetType.GetProperties.ToList.ForEach(Sub(x) Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, Nothing)}")) 

这是我如何做到的。

 foreach (var fi in typeof(CustomRoles).GetFields()) { var propertyName = fi.Name; }