Author:水如烟
Public Class MemberInfoFunction
Private Sub New()
End Sub
Public Shared Function TypeHasFields(ByVal t As Type) As Boolean
Return t.GetFields.Length > 0
End Function
Public Shared Function TypeHasMember(ByVal t As Type, ByVal memberName As String) As Boolean
Return t.GetMember(memberName) IsNot Nothing
End Function
Public Shared Function GetFieldResult(ByVal item As Object, ByVal fieldName As String) As Object
Dim mFieldInfo As Reflection.FieldInfo = GetFieldInfo(fieldName, item)
If mFieldInfo Is Nothing Then Return Nothing
Return mFieldInfo.GetValue(item)
End Function
Public Shared Sub SetFieldValue(ByVal item As Object, ByVal fieldName As String, ByVal Value As Object)
Dim mFieldInfo As Reflection.FieldInfo = GetFieldInfo(fieldName, item)
If mFieldInfo Is Nothing Then Return
If Value Is DBNull.Value Then Value = Nothing
mFieldInfo.SetValue(item, Value)
Public Shared Function GetPropertyResult(ByVal item As Object, ByVal propertyName As String, ByVal index As Object()) As Object
Dim mPropertyInfo As Reflection.PropertyInfo = GetPropertyInfo(propertyName, item)
If mPropertyInfo Is Nothing Then Return Nothing
If Not mPropertyInfo.CanRead Then Return Nothing
Return mPropertyInfo.GetValue(item, index)
End Function
Public Shared Sub SetPropertyValue(ByVal item As Object, ByVal propertyName As String, ByVal Value As Object, ByVal index As Object())
Dim mPropertyInfo As Reflection.PropertyInfo = GetPropertyInfo(propertyName, item)
If mPropertyInfo Is Nothing Then Return
If Not mPropertyInfo.CanWrite Then Return
If Value Is DBNull.Value Then Value = Nothing
mPropertyInfo.SetValue(item, System.Convert.ChangeType(Value, Type.GetTypeCode(mPropertyInfo.PropertyType)), index)
Public Shared Function GetMethodResult(ByVal item As Object, ByVal methodName As String, ByVal paras As Object()) As Object
Dim mMethodInfo As Reflection.MethodInfo = Nothing
mMethodInfo = ObjectType(item).GetMethod(methodName, gbinding)
If mMethodInfo Is Nothing Then Return Nothing
Dim mResult As Object = mMethodInfo.Invoke(item, paras)
Return mResult
End Function
Public Shared Sub AddEventHandler(ByVal eventName As String, ByVal target As Object, ByVal methodNameOfinstance As String, ByVal instance As Object)
If instance Is Nothing Then Return
Dim mEventInfo As Reflection.EventInfo = GetEventInfo(eventName, target)
Dim mMethodInfo As Reflection.MethodInfo = GetMethodInfo(methodNameOfinstance, instance)
If mEventInfo Is Nothing OrElse mMethodInfo Is Nothing Then Return
Dim d As [Delegate] = GetDelegate(mEventInfo, instance, mMethodInfo)
mEventInfo.AddEventHandler(target, d)
Public Shared Sub RemoveEventHandler(ByVal eventName As String, ByVal target As Object, ByVal methodNameOfinstance As String, ByVal instance As Object)
If instance Is Nothing Then Return
Dim mEventInfo As Reflection.EventInfo = GetEventInfo(eventName, target)
Dim mMethodInfo As Reflection.MethodInfo = GetMethodInfo(methodNameOfinstance, instance)
If mEventInfo Is Nothing OrElse mMethodInfo Is Nothing Then Return
Dim d As [Delegate] = GetDelegate(mEventInfo, instance, mMethodInfo)
mEventInfo.RemoveEventHandler(target, d)
Public Shared Function GetDelegate(ByVal eventName As String, ByVal target As Object, ByVal methodNameOfinstance As String, ByVal instance As Object) As [Delegate]
Dim mEventInfo As Reflection.EventInfo = GetEventInfo(eventName, target)
Dim mMethodInfo As Reflection.MethodInfo = GetMethodInfo(methodNameOfinstance, instance)
Return GetDelegate(mEventInfo, instance, mMethodInfo)
End Function
Public Shared Function GetDelegate(ByVal eventinfo As Reflection.EventInfo, ByVal instance As Object, ByVal methodinfo As Reflection.MethodInfo) As [Delegate]
Dim d As [Delegate]
If IsType(instance) Then
d = [Delegate].CreateDelegate(eventinfo.EventHandlerType, Nothing, methodinfo)
Else
d = [Delegate].CreateDelegate(eventinfo.EventHandlerType, instance, methodinfo)
End If
Return d
End Function
Public Shared Function GetEventInfo(ByVal name As String, ByVal target As Object) As Reflection.EventInfo
Return ObjectType(target).GetEvent(name, gBinding)
End Function
Public Shared Function GetMethodInfo(ByVal name As String, ByVal instance As Object) As Reflection.MethodInfo
Return ObjectType(instance).GetMethod(name, gBinding)
End Function
Public Shared Function GetPropertyInfo(ByVal name As String, ByVal instance As Object) As Reflection.PropertyInfo
Return ObjectType(instance).GetProperty(name, gBinding)
End Function
Public Shared Function GetFieldInfo(ByVal name As String, ByVal instance As Object) As Reflection.FieldInfo
Return ObjectType(instance).GetField(name, gBinding)
End Function
Public Shared Function ObjectType(ByVal instance As Object) As Type
Dim mType As Type
If IsType(instance) Then
mType = CType(instance, Type)
Else
mType = instance.GetType
End If
Return mType
End Function
Private Shared Function IsType(ByVal instance As Object) As Boolean
Return instance.GetType.IsSubclassOf(GetType(Type))
End Function
Private Const gBinding As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.CreateInstance
End Class
End Namespace