Here is some generic reflection code that we use. The methods do what they
say. There is some logic in there specific to scenarios we've ran across in
our projects, it works pretty well. Good luck.
Public Shared Function GetDefaultValue(ByVal voObject As Object) As Object
Dim voReturnVal As Object
Try
'Use reflection to get value of default property for a given object.
If voObject Is Nothing _
OrElse voObject.GetType.IsPrimitive _
OrElse voObject.GetType.Equals(Type.GetType("System.String")) _
OrElse voObject.GetType.Equals(Type.GetType("System.DateTime")) _
OrElse voObject.GetType.Equals(Type.GetType("System.Decimal")) _
OrElse voObject.GetType.Equals(Type.GetType("System.DBNull")) Then
'No default property. Just return value passed in.
voReturnVal = voObject
ElseIf voObject.GetType.Equals(Type.GetType("System.Guid")) Then
If voObject.ToString = String.Empty Then
voReturnVal = System.DBNull.Value
Else
voReturnVal = voObject.ToString
End If
Else
'Get default property value.
voReturnVal = voObject.GetType.InvokeMember(String.Empty, _
BindingFlags.Default Or BindingFlags.Public Or _
BindingFlags.IgnoreCase Or BindingFlags.Instance Or _
BindingFlags.GetProperty, _
Nothing, _
voObject, _
Nothing)
If voReturnVal.GetType.ToString = "System.__ComObject" Then
voReturnVal = GetDefaultValue(voReturnVal)
End If
End If
Return voReturnVal
Catch ex As System.MissingMethodException
Return voObject
Catch ex As System.Reflection.TargetInvocationException
Return voObject
Finally
voReturnVal = Nothing
End Try
End Function
Public Overloads Shared Function GetPropertyValue(ByVal voObject As Object,
_
ByVal vsProperty As String) As Object
Return voObject.GetType.InvokeMember(name:=vsProperty, _
invokeAttr:=BindingFlags.GetProperty, _
binder:=Nothing, _
target:=voObject, _
args:=Nothing)
End Function
Public Overloads Shared Function GetPropertyValue(ByVal voObject As Object,
_
ByVal vsProperty As String, _
ByVal viIndex As Integer) As Object
voObject = GetPropertyValue(voObject, vsProperty)
Return voObject(viIndex)
End Function
Public Shared Sub PutDefaultValue(ByRef roObject As Object, _
ByVal voValue As Object)
Try
If Not IsNothing(roObject) AndAlso Marshal.IsComObject(roObject) Then
Call roObject.GetType.InvokeMember(name:=String.Empty, _
invokeAttr:=BindingFlags.Default Or BindingFlags.Public Or _
BindingFlags.IgnoreCase Or BindingFlags.Instance Or _
BindingFlags.SetProperty, _
binder:=Nothing, _
target:=roObject, _
args:=New Object() {voValue})
Else
roObject = voValue
End If
Catch ex As System.Reflection.TargetInvocationException
'Eat the exception. There is no default value to put back.
End Try
End Sub
Public Shared Sub PutPropertyValue(ByRef roObject As Object, _
ByVal vsProperty As String, _
ByVal voValue As Object)
Call roObject.GetType.InvokeMember(name:=vsProperty, _
invokeAttr:=BindingFlags.SetProperty, _
binder:=Nothing, _
target:=roObject, _
args:=New Object() {voValue})
End Sub
Public Overloads Shared Function GetFieldValue(ByVal voObject As Object, _
ByVal vsField As String) As Object
Return voObject.GetType.InvokeMember(name:=vsField, _
invokeAttr:=BindingFlags.GetField, _
binder:=Nothing, _
target:=voObject, _
args:=Nothing)
End Function
Doudou - 03 Apr 2006 10:08 GMT
Thanks, it helps me but my first problem is that I would loop on each
property of MsProject task object which have a lot of properties.
So I would like to find a method to list "dynamically" all properties
of a task object and after, I can use your functions.
VB6 To .NET - 03 Apr 2006 14:57 GMT
Can't help you there. Maybe someone else can jump in and show a way to
dynamically get all objects of a class. It's beyond me though.