How to iterate through each property of a custom vb.net object?
By using reflection you can do that. In C# it looks like that;
PropertyInfo[] propertyInfo = myobject.GetType().GetProperties();
Added a VB.Net translation:
Dim info() As PropertyInfo = myobject.GetType().GetProperties()
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 IfNext
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.
System.Reflection is "heavy-weight", i always implement a lighter method first..
//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