Reflection is a set of classes that allow you to access and manipulate assemblies and modules and the types and the metadata that they contain. For example, you can use reflection to enumerate loaded assemblies, modules, and classes and the methods, properties, fields, and events that each type exposes. Reflection plays a fundamental role in the Microsoft .NET Framework and works as a building block for other important portions of the runtime. The runtime uses reflection in many circumstances, such as to enumerate fields when a type is being serialized or is being marshaled to another process or another machine. Microsoft Visual Basic transparently uses reflection whenever you access an object’s method through late binding.
Example
Imports System.Reflection
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MethodObj As System.Reflection.MethodInfo
Dim MessageDemo As New Demo()
For Each MethodObj In MessageDemo.GetType.GetMethods()
Dim Attr As Attribute
Dim szMessage As String = String.Empty
For Each Attr In MethodObj.GetCustomAttributes(False)
szMessage += MethodObj.Name & vbCrLf
'Console.WriteLine(Attr)
szMessage += Attr.ToString & vbCrLf
szMessage += CType(Attr, UserName).Name & vbCrLf
Next
MessageBox.Show(szMessage)
Next
End Sub
End Class
Class UserName
Inherits Attribute
Public Name As String
Public Sub New(ByVal Name As String)
MyBase.New()
Me.Name = Name
End Sub
End Class
Class Demo
Console.WriteLine("Message")
End Sub
Console.WriteLine("Hello")
End Sub
End Class
Get Class Member and Property Information from base and inherited Class
Imports System.Reflection
Public Class MainClass
Public Shared Sub Main()
Dim Book = New Derived()
Dim Member As MemberInfo
Console.WriteLine("Members:")
For Each Member In Book.GetType.GetMembers()
Console.WriteLine(Member.Name & " " & Member.MemberType)
Next
Dim PropertyObj As PropertyInfo
Console.WriteLine("Properties:")
For Each PropertyObj In Book.GetType.GetProperties()
Console.WriteLine(PropertyObj.Name & " " & PropertyObj.PropertyType.ToString())
Next
Dim MethodObj As MethodInfo
Console.WriteLine("Methods:")
For Each MethodObj In Book.GetType.GetMethods()
Console.WriteLine(MethodObj.Name & " " & MethodObj.ReturnType.ToString())
Next
Dim EventObj As EventInfo
Console.WriteLine("Events:")
For Each EventObj In Book.GetType.GetEvents()
Console.WriteLine(EventObj.Name & " " & EventObj.IsMulticast)
Next
Dim InterfaceObj As Type
Console.WriteLine("Events:")
For Each InterfaceObj In Book.GetType.GetInterfaces()
Console.WriteLine(InterfaceObj.Name)
Next
End Sub
End Class
Class Base
Public ProductID As String
Public Weight As Double
Private ProductPrice As Double
Public Sub New()
End Sub
Public ReadOnly Property Price() As Double
Get
Return 0
End Get
End Property
End Class
Class Derived
Inherits Base
Implements IFormattable
Public Title As String
Public Author As String
Public Publisher As String
Public Overridable Overloads Function ToString(ByVal _
Format As String, ByVal Provider As IFormatProvider) _
As String Implements IFormattable.ToString
ToString = Title
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
Get Method Information
Imports System.Reflection
Public Class MainClass
Public Shared Sub Main()
Dim SomeObj = New Demo()
Dim MethodObj As System.Reflection.MethodInfo
Console.WriteLine()
Console.WriteLine("Methods:")
For Each MethodObj In SomeObj.GetType.GetMethods()
Console.WriteLine(MethodObj.Name & " " & MethodObj.ReturnType.ToString())
Dim Param As ParameterInfo
For Each Param In MethodObj.GetParameters()
Console.WriteLine(Param.Name & " " & Param.ParameterType.ToString())
Next
Console.WriteLine()
Next
End Sub
End Class
Class Demo
Public Sub A()
End Sub
Public Sub B(ByVal Msg As String)
End Sub
Public Function C(ByVal A As Integer, ByVal B As Integer) As Integer
End Function
Public Sub D(ByVal A As Double, ByVal B As Double, ByVal C As Double)
End Sub
End Class
Add a comment