现在的位置: 首页 > 综合 > 正文

LzmTW.uSystem.uReflection+MemberInfoFunction

2013年10月12日 ⁄ 综合 ⁄ 共 6181字 ⁄ 字号 评论关闭

Author:水如烟  

Namespace LzmTW.uSystem.uReflection
    
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 StringAs Boolean
            
Return t.GetMember(memberName) IsNot Nothing
        
End Function

        Public Shared Function GetFieldResult(ByVal item As ObjectByVal fieldName As StringAs 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 ObjectByVal fieldName As StringByVal 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)
        End Sub

        Public Shared Function GetPropertyResult(ByVal item As ObjectByVal propertyName As StringByVal 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 ObjectByVal propertyName As StringByVal Value As ObjectByVal 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)
        End Sub

        Public Shared Function GetMethodResult(ByVal item As ObjectByVal methodName As StringByVal 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 StringByVal target As ObjectByVal methodNameOfinstance As StringByVal 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)
        End Sub

        Public Shared Sub RemoveEventHandler(ByVal eventName As StringByVal target As ObjectByVal methodNameOfinstance As StringByVal 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)
        End Sub

        Public Shared Function GetDelegate(ByVal eventName As StringByVal target As ObjectByVal methodNameOfinstance As StringByVal instance As ObjectAs [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 ObjectByVal 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 StringByVal target As ObjectAs Reflection.EventInfo

            Return ObjectType(target).GetEvent(name, gBinding)
        
End Function

        Public Shared Function GetMethodInfo(ByVal name As StringByVal instance As ObjectAs Reflection.MethodInfo

            Return ObjectType(instance).GetMethod(name, gBinding)
        
End Function

        Public Shared Function GetPropertyInfo(ByVal name As StringByVal instance As ObjectAs Reflection.PropertyInfo

            Return ObjectType(instance).GetProperty(name, gBinding)
        
End Function

        Public Shared Function GetFieldInfo(ByVal name As StringByVal instance As ObjectAs Reflection.FieldInfo

            Return ObjectType(instance).GetField(name, gBinding)
        
End Function

        Public Shared Function ObjectType(ByVal instance As ObjectAs 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 ObjectAs 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

 

抱歉!评论已关闭.