如何遍历自定义vb.net对象的每个属性?

时间:2021-06-26 22:35:00

How can I go through each of the properties in my custom object? It is not a collection object, but is there something like this for non-collection objects?

如何检查自定义对象中的每个属性?它不是一个集合对象,但是对于非集合对象是否有类似的东西?

For Each entry as String in myObject
    ' Do stuff here...
Next

There are string, integer and boolean properties in my object.

对象中有字符串、整型和布尔属性。

4 个解决方案

#1


60  

By using reflection you can do that. In C# it looks like that;

通过使用反射,你可以做到这一点。在c#中是这样的;

PropertyInfo[] propertyInfo = myobject.GetType().GetProperties();

Added a VB.Net translation:

添加一个VB。Net翻译:

Dim info() As PropertyInfo = myobject.GetType().GetProperties()

#2


41  

You can use System.Reflection namespace to query information about the object type.

您可以使用系统。反射命名空间,用于查询关于对象类型的信息。

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))
   End If
Next

Please note that it is not suggested to use this approach instead of collections in your code. Reflection is a performance intensive thing and should be used wisely.

请注意,不建议在代码中使用这种方法而不是集合。反射是一种性能密集型的东西,应该明智地使用它。

#3


5  

System.Reflection is "heavy-weight", i always implement a lighter method first..

系统。反射是“重”的,我总是先实现一个轻的方法。

//C#

/ / 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

“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

#4


1  

You can use reflection... With Reflection you can examine every member of a class (a Type), proeprties, methods, contructors, fields, etc..

您可以使用反射……通过反射,您可以检查类(类型)、proeprties、方法、构造函数、字段等的每个成员。

using System.Reflection;

Type type = job.GetType();
    foreach ( MemberInfo memInfo in type.GetMembers() )
       if (memInfo is PropertyInfo)
       {
            // Do Something
       }

#1


60  

By using reflection you can do that. In C# it looks like that;

通过使用反射,你可以做到这一点。在c#中是这样的;

PropertyInfo[] propertyInfo = myobject.GetType().GetProperties();

Added a VB.Net translation:

添加一个VB。Net翻译:

Dim info() As PropertyInfo = myobject.GetType().GetProperties()

#2


41  

You can use System.Reflection namespace to query information about the object type.

您可以使用系统。反射命名空间,用于查询关于对象类型的信息。

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))
   End If
Next

Please note that it is not suggested to use this approach instead of collections in your code. Reflection is a performance intensive thing and should be used wisely.

请注意,不建议在代码中使用这种方法而不是集合。反射是一种性能密集型的东西,应该明智地使用它。

#3


5  

System.Reflection is "heavy-weight", i always implement a lighter method first..

系统。反射是“重”的,我总是先实现一个轻的方法。

//C#

/ / 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

“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

#4


1  

You can use reflection... With Reflection you can examine every member of a class (a Type), proeprties, methods, contructors, fields, etc..

您可以使用反射……通过反射,您可以检查类(类型)、proeprties、方法、构造函数、字段等的每个成员。

using System.Reflection;

Type type = job.GetType();
    foreach ( MemberInfo memInfo in type.GetMembers() )
       if (memInfo is PropertyInfo)
       {
            // Do Something
       }