Find all the properties for an object
Im new to VB and have been thrown into using a massive web service. I am constantly receiving back objects and it would be very useful be able to print out al开发者_运维百科l of their properties. Is there a way in VB to get all the properties of an object and print them out (to console)?
Im thinking this would need some type of reflection, but it would be nice if there was some kind of built in mechanism for this.
Any ideas?
Read your question again and got a little more enlightened :)
http://msdn.microsoft.com/en-us/library/aa332493(v=vs.71).aspx
You can use this to get the properties of an object:
Public Shared Sub Main()
Dim myType As Type = GetType(MyTypeClass)
' Get the public properties.
Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length.ToString())
' Display the public properties.
DisplayPropertyInfo(myPropertyInfo)
End Sub 'Main
Public Shared Sub DisplayPropertyInfo(ByVal myPropertyInfo() As PropertyInfo)
' Display the information for all properties.
Dim i As Integer
For i = 0 To myPropertyInfo.Length - 1
Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())
Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())
Next i
End Sub 'DisplayPropertyInfo
Hope this helps!
You can use Type.GetProperties This returns an array with PropertyInfo object which each represent a property on your object. The PropertyInfo object has a Name object which holds the name of the property on your object.
In a SOA world you can achieve that with the WSDL (WSDL Types)
you can find all the information you need as the nature of the service in generals states that they must be self contained and auto descriptive
for more info:
http://www.w3schools.com/wsdl/wsdl_documents.asp
精彩评论