下午没事写了小东西,来复习下反射机制
1 首先建一个项目,名字叫“Net反射机制”,注意不能改成其他,不然不好验证
2 在窗体上放几个按钮,一个文本框,如图
项目中添加一个类 用来反射他:
Public Class ROBJ
Private result As Integer = 0
Public Sub New(ByVal _set As Integer)
result = _set
End Sub
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
Public Property MyResult() As Integer
Get
Return result
End Get
Set(ByVal value As Integer)
result = value
End Set
End Property
End Class
项目代码如下
Imports System.Reflection
Imports System.Text
Public Class Form1
'反射是一个很好工具,有时候可以突破面向对象的种种限制
'net程序层次:程序域—程序集—模块—类型—成员
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub 得到程序域所有程序集(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim asm As Assembly() = System.AppDomain.CurrentDomain.GetAssemblies()
'具体引用外部dll可以 Assembly.LoadFrom (“ReflectionExample.exe”)
Dim a As Assembly
Dim result As New StringBuilder
For Each a In asm
result.Append(a.FullName & vbCrLf)
Next
Me.RichTextBox1.Text = result.ToString
End Sub
Private Sub 程序集的Load方式(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
REM: Assembly.Load (AssemblyName) 在给定程序集的 AssemblyName 的情况下,加载程序集
REM Assembly.Load (Byte[]) 加载带有基于通用对象文件格式 (COFF) 的图像的程序集,该图像包含已发出的程序集。将该程序集加载到调用方的域
REM Assembly.Load (String) 通过给定程序集的长格式名称加载程序集
Dim result As New StringBuilder
'Dim asm As Assembly = Assembly.Load("Net反射机制, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
'Dim asm As Assembly = Assembly.LoadFrom("Net反射机制.exe")
Dim asm As Assembly = Assembly.Load("Net反射机制") '使用程序集来查找
Dim _t As Type() = asm.GetTypes
Dim _type As Type
For Each _type In _t
result.Append(_type.Name & vbCrLf)
Next
Me.RichTextBox1.Text = result.ToString
End Sub
Private Sub 反射类型成员(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim result As New StringBuilder
Dim asm As Assembly = Assembly.Load("Net反射机制")
Dim _t As Type() = asm.GetTypes
Dim _type As Type
For Each _type In _t
If _type.Name = "ROBJ" Then
result.Append(_type.Name & vbCrLf)
'BindingFlags.DeclaredOnly说明是自身的方法
Dim flag As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or BindingFlags.Instance Or BindingFlags.DeclaredOnly
Dim _method As MethodInfo() = _type.GetMethods(flag)
Dim m As MethodInfo
For Each m In _method
result.Append("方法 " & m.Name & vbCrLf)
Dim _pp As ParameterInfo() = m.GetParameters()
Dim 参数 As ParameterInfo
For Each 参数 In _pp
Dim ooi As String = IIf(Not 参数.IsOut, "输入参数", "输出参数")
result.Append("参数 " & 参数.Name & "【" & ooi & "】 参数类型 " & 参数.ParameterType.ToString & " " & vbCrLf)
Next
Next
Dim _property As PropertyInfo() = _type.GetProperties(flag)
Dim p As PropertyInfo
For Each p In _property
result.Append("属性 " & p.Name & vbCrLf)
Next
Dim _FieldInfo As FieldInfo() = _type.GetFields(flag)
Dim f As FieldInfo
For Each f In _FieldInfo
result.Append("字段 " & f.Name & vbCrLf)
Next
End If
Next
Me.RichTextBox1.Text = result.ToString
End Sub
Private Sub 反射来动态创建对象(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim asm As Assembly = Assembly.Load("Net反射机制") '使用程序集来查找
Dim _t As Type() = asm.GetTypes
Dim _type As Type
For Each _type In _t
If _type.Name = "ROBJ" Then
Dim p As Integer = 811221
Dim obj As Object = System.Activator.CreateInstance(_type, p)
Me.RichTextBox1.Text = obj.MyResult()
End If
Next
End Sub
Private Sub 反射来动动态调用方法(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim asm As Assembly = Assembly.Load("Net反射机制") '使用程序集来查找
Dim _t As Type() = asm.GetTypes
Dim _type As Type
For Each _type In _t
If _type.Name = "ROBJ" Then
Dim p As Integer = 811221
Dim obj As Object = System.Activator.CreateInstance(_type, p)
Dim m As MethodInfo = _type.GetMethod("Add")
Dim result As Integer = m.Invoke(obj, New Object() {1, 33})
Me.RichTextBox1.Text = result
End If
Next
End Sub
End Class