ลองไปดาวน์โหลด Version สำหรับ .NET นั้น ๆ มาครับ มันน่าจะออกใหม่แล้วครับ LIB ตัวนี้ค่อนข้างจะอัพเดด
Date :
2016-02-17 18:29:45
By :
mr.win
No. 6
Guest
Credit : หน้าฮี
โค๊ดใช้งานจริง
Code (VB.NET)
'Credit : หน้าฮี
'Date Created : 2009-11-11
'Date Modify : 2012-11-11
Imports System.Reflection
Public Class ReflectionMgr
Private Shared flags As BindingFlags = BindingFlags.Public Or
BindingFlags.NonPublic Or
BindingFlags.Static Or
BindingFlags.Instance Or
BindingFlags.DeclaredOnly
Sub New()
End Sub
''' <summary>
''' runtime load assembly
''' </summary>
''' <param name="fullname">"yourPath/yourDLL.dll"</param>
''' <returns></returns>
''' <remarks>Web Application --> System.Web.HttpContext.Current.Server.MapPath(fullname)</remarks>
Public Shared Function LoadAssembly(ByVal fullname As String) As System.Reflection.Assembly
Try
Return Assembly.LoadFrom(fullname)
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' runtime get Type
''' </summary>
''' <param name="ass">Assembly object example LoadAssembly(ByVal fullname As String) As System.Reflection.Assembly</param>
''' <param name="fullname">string class name example "SEX_Model.usrBSCUnit"</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function LoadType(ByVal ass As Assembly, fullname As String) As System.Type
Try
Return ass.CreateInstance(fullname).GetType()
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' return Private property only
''' </summary>
''' <param name="t">LoadType(ByVal ass As Assembly, fullname As String) As System.Type</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetFieldInfo(ByVal t As System.Type) As FieldInfo()
Try
Return t.GetFields(flags)
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' return Inherits Property
''' </summary>
''' <param name="t"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetBaseTypeFieldInfo(ByVal t As System.Type) As FieldInfo()
Try
Return t.BaseType().GetFields(flags)
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' Get All field Inherits/Private Property
''' </summary>
''' <param name="t"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetAllFieldInfo(ByVal t As System.Type) As IEnumerable(Of FieldInfo) 'As FieldInfo()
'2009-11-18'Fixed Bug
'Public Shared Function GetAllFieldInfo(ByVal t As System.Type) As FieldInfo() to As Object
'Error Unable to cast object of type
'<UnionIterator>d_88'1[System.Reflection.FieldInfo]' to type
'System.Reflection.Fieldinfo'.
Try
'Dim x = t.GetFields(flags).Union(t.BaseType().GetFields(flags))
'Dim y = t.GetFields(flags).AsEnumerable.Union(t.BaseType().GetFields(flags).AsEnumerable)
'Return t.GetFields(flags).AsEnumerable().Union(t.BaseType().GetFields(flags))
Return t.GetFields(flags).Union(t.BaseType().GetFields(flags))
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
'''
''' </summary>
''' <param name="t"></param>
''' <param name="MethodName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetMethodInfo(ByVal t As System.Type, ByVal MethodName As String) As System.Reflection.MethodInfo
Try
'Fix bug ambiguous overload function/method 2012-12-28
Return t.GetMethod(MethodName, New Type() {})
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
'''
''' </summary>
''' <param name="t"></param>
''' <param name="MethodName"></param>
''' <param name="paraArray"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetMethodInfo(ByVal t As System.Type, ByVal MethodName As String, ByVal paraArray() As System.Type) As System.Reflection.MethodInfo
Try
Return t.GetMethod(MethodName, paraArray)
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
'''
''' </summary>
''' <param name="t"></param>
''' <param name="MethodName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetMethodsInfo(ByVal t As System.Type, ByVal MethodName As String) As System.Reflection.MethodInfo()
Try
Return t.GetMethods()
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' Dim x As New SEX_Model.usrBSCUnit
''' x.GetType().ToString() = "SEX_Model.usrBSCUnit"
''' </summary>
''' <param name="m"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function InvokeMethod(ByVal m As MethodInfo) As Object
Try
Return m.Invoke(New Object() {}, New Object() {})
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' InvokeMethod in assembly
''' </summary>
''' <param name="m">GetMethodInfo(ByVal t As System.Type, ByVal MethodName As String) As System.Reflection.MethodInfo</param>
''' <param name="args">Dim args As Object() = New Object() {1, "car01", Today}</param>
''' <returns>Object</returns>
''' <remarks></remarks>
Public Shared Function InvokeMethod(ByVal m As MethodInfo, ByVal args() As Object) As Object
Try
Return m.Invoke(New Object() {}, args)
'Return m.Invoke(Nothing, args)
'Return m.Invoke(x, args) 'Invoking a method with ByRef parameters
Catch ex As Exception
Return Nothing
'KM
'Dim a As Object() = New Object() {1, "car01", Today}
'Dim b As String() = New String() {"a", "b", "c"}
'Dim c() As String = New String() {"a", "b", "c"}
'Dim x As DateTime = #12/31/2009 8:30:00 AM# 'ยึดรูปแบบ US MM-dd-yyyy เสมอ
'Dim y As DateTime = New DateTime(2099, 11, 11, 8, 30, 0, 0, DateTimeKind.Utc)
'New Object() = Nothing
'New Object() {} = Nothing
End Try
End Function
End Class