The following code does the same in vb.net. I also show how to get attributes and parameters of an object.
Please correct me if I am wrong.
------------------------------------------------
Imports System.Reflection Imports System.Collections.Generic Public Class myReflections Public Shared Function GetProperties(ByVal ob As Object) As String Dim theType As Type = ob.GetType ' Get the properties in the object Dim properties() As PropertyInfo = theType.GetProperties() ' loop through the properties Dim msg As String = "" For Each prop As PropertyInfo In properties msg &= prop.Name + " is of type " + prop.PropertyType.ToString() + " and " If prop.CanRead Then msg &= "Can be Read and " Else msg &= " Cannot be Read and " End If If prop.CanWrite Then msg &= "Can be Written " Else msg &= "Cannot be Written " End If msg &= vbCrLf Next Return msg End Function Public Shared Function GetParameters(ByVal ob As Object) As String Dim myStr As String = "", theType As Type = ob.GetType ' sample call GetParameters(GetType(System.Math)) ' Get the list of methods Dim myMethods() As MethodInfo = theType.GetMethods ' Loop thru the methods Dim MethodItem As MethodInfo For Each MethodItem In myMethods myStr = MethodItem.Name & " takes the following parameters: " ' Get the parameters for each method Dim myParms() As ParameterInfo = MethodItem.GetParameters ' point out if there are no parameters If myParms.Length = 0 Then myStr &= "
End If
Dim ParmItem As ParameterInfo
' output some details for each parameter
For Each ParmItem In myParms
With ParmItem
myStr &= "
.ParameterType.ToString
End With
myStr &= "<&47;ul>"
Next
Next
Return myStr
End Function
Public Shared Function CompareCorresponding(ByVal Ob1 As Object, ByVal ob2 As Object) As Boolean
Try
Dim result As Boolean = True
If Ob1.GetType.Name <> ob2.GetType.Name Then
Throw New ApplicationException("Objects of differing type cannot be compared." & vbCrLf & "Attempted to compare " & Ob1.GetType.Name & " and " & ob2.GetType.Name)
End If
Dim prop1() As PropertyInfo = Ob1.GetType.GetProperties
Dim prop2() As PropertyInfo = ob2.GetType.GetProperties
Dim missed As String = ""
Dim i As Integer = -1
Debug.Assert(prop1.Length = prop2.Length, "Number of properties varies between the objects!")
Dim v1 As String = "", v2 As String = ""
For i = 0 To prop1.Length - 1
Debug.Assert(prop1(i).Name = prop2(i).Name, "The order of the properties is not the same for the objects")
Dim p1 As PropertyInfo = prop1(i), p2 As PropertyInfo = prop2(i)
'debug.print(p1.Name)
Select Case p1.PropertyType.Name
Case "Int32", "System.Int16", "Int64"
v1 = GetV(p1, Ob1)
v2 = GetV(p2, ob2)
Case "String[]"
v1 = GetV(p1, Ob1)
v2 = GetV(p2, ob2)
Case "String"
v1 = GetV(p1, Ob1)
v2 = GetV(p2, ob2)
Case "Boolean"
v1 = GetV(p1, Ob1)
v2 = GetV(p2, ob2)
Case Else
missed &= p1.Name & "(" & p1.PropertyType.Name & "),"
'debug.print(p1.Name & "(" & p1.PropertyType.Name & ") not compared")
Continue For
End Select
'debug.print(v1)
'debug.print(v2)
result = result And String.Compare(v1, v2, True) = 0
Debug.Assert(result = True)
Next
'debug.print(missed)
Return result
Catch ex As Exception
Throw New ApplicationException(ex.Message)
End Try
End Function
Private Shared Function GetV(ByVal p As PropertyInfo, ByVal o As Object) As String
Dim result As String = "", len As Integer = p.GetIndexParameters().Length
Dim fRes As String = ""
Dim myIndex As Object = Nothing
Dim myIndices() As Object = Nothing
With p
result = (p.GetValue(o, Nothing)).ToString
If result = "System.String[]" Then
myIndex = p.GetValue(o, myIndices)
For i As Integer = 0 To CType(myIndex, System.Array).Length - 1
result &= CType(myIndex, String())(i)
Next
ElseIf result = "Int32[]" OrElse result = "Int64[]" Then
myIndex = p.GetValue(o, myIndices)
For i As Integer = 0 To CType(myIndex, System.Array).Length - 1
result &= CType(myIndex, String())(i)
Next
End If
End With
Return result
End Function
End Class
No comments:
Post a Comment