1. 动态创建对象
CreatingAnObjectDinamically
Imports System.Reflection
Module CreatingAnObjectDinamically
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Sub a()
Dim o As Object = Activator.CreateInstance(type)
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub b()
Dim o As Object = Activator.CreateInstance(type, {"Joe", "Doe"} )
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub c()
Dim o As Object =
type.InvokeMember(name:="",
invokeAttr:=BindingFlags.CreateInstance,
binder:=Nothing,
target:=Nothing,
args:={"Joe", "Doe"})
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub d()
Dim argTypes() As Type = {GetType(String), GetType(String)}
Dim ci As ConstructorInfo = type.GetConstructor(argTypes)
Dim o As Object = ci.Invoke({"Joe", "Doe"})
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub e()
Dim arrType As Type = GetType(Double).MakeArrayType()
Dim arr As Array = DirectCast(Activator.CreateInstance(arrType, 10), Array)
arr.SetValue(123.45, 0)
Console.WriteLine("{0} {1} elements", arr.Length, arr.GetValue(0).GetType.Name)
Console.WriteLine(arr.GetValue(0))
End Sub
End Module
Module CreatingAnObjectDinamically
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Sub a()
Dim o As Object = Activator.CreateInstance(type)
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub b()
Dim o As Object = Activator.CreateInstance(type, {"Joe", "Doe"} )
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub c()
Dim o As Object =
type.InvokeMember(name:="",
invokeAttr:=BindingFlags.CreateInstance,
binder:=Nothing,
target:=Nothing,
args:={"Joe", "Doe"})
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub d()
Dim argTypes() As Type = {GetType(String), GetType(String)}
Dim ci As ConstructorInfo = type.GetConstructor(argTypes)
Dim o As Object = ci.Invoke({"Joe", "Doe"})
Console.WriteLine("{0} object has been created", o.GetType().Name)
End Sub
Sub e()
Dim arrType As Type = GetType(Double).MakeArrayType()
Dim arr As Array = DirectCast(Activator.CreateInstance(arrType, 10), Array)
arr.SetValue(123.45, 0)
Console.WriteLine("{0} {1} elements", arr.Length, arr.GetValue(0).GetType.Name)
Console.WriteLine(arr.GetValue(0))
End Sub
End Module
2. 访问成员
AccessingMembers
Sub AgePi()
Dim age As PropertyInfo = type.GetProperty("Age")
age.SetValue(obj:=obj,
value:=35S,
index:=Nothing)
Console.WriteLine(age.GetValue(obj:=obj, index:=Nothing))
End Sub
Sub NotesPi()
Dim notes As PropertyInfo = type.GetProperty("Notes")
notes.SetValue(obj, "Tell John about the briefing", {1})
Console.WriteLine(notes.GetValue(obj, {1}))
End Sub
Sub m()
Dim mi As MethodInfo = type.GetMethod("SendEmail")
mi.Invoke(obj, {"This is a message", 3})
mi.Invoke(obj, {"This is a message", type.Missing})
mi.Invoke(obj, {"This is a message", mi.GetParameters(1).DefaultValue})
Try
mi.Invoke(obj, {"This is a message", -1})
Catch ex As TargetInvocationException
Console.WriteLine(ex.InnerException.Message)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
Imports System.Reflection
Module AccessingMembers
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Dim obj As Object = Activator.CreateInstance(type, {"Joe", "Doe"})
Sub f()
Dim firstName As FieldInfo = type.GetField("FirstName")
firstName.SetValue(obj, "Robert")
Module AccessingMembers
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Dim obj As Object = Activator.CreateInstance(type, {"Joe", "Doe"})
Sub f()
Dim firstName As FieldInfo = type.GetField("FirstName")
firstName.SetValue(obj, "Robert")
Console.WriteLine(firstName.GetValue(obj))
End Sub
End Sub
Sub AgePi()
Dim age As PropertyInfo = type.GetProperty("Age")
age.SetValue(obj:=obj,
value:=35S,
index:=Nothing)
Console.WriteLine(age.GetValue(obj:=obj, index:=Nothing))
End Sub
Sub NotesPi()
Dim notes As PropertyInfo = type.GetProperty("Notes")
notes.SetValue(obj, "Tell John about the briefing", {1})
Console.WriteLine(notes.GetValue(obj, {1}))
End Sub
Sub m()
Dim mi As MethodInfo = type.GetMethod("SendEmail")
mi.Invoke(obj, {"This is a message", 3})
mi.Invoke(obj, {"This is a message", type.Missing})
mi.Invoke(obj, {"This is a message", mi.GetParameters(1).DefaultValue})
Try
mi.Invoke(obj, {"This is a message", -1})
Catch ex As TargetInvocationException
Console.WriteLine(ex.InnerException.Message)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
3. InvokeMember方法
InvokeMember
Imports System.Reflection
Module InvokeMember
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Dim arguments() As Object = {"Joe", "Doe"}
Dim obj As Object = type.InvokeMember("", BindingFlags.CreateInstance, Nothing, Nothing, arguments)
Sub InvokeMemberMethod()
type.InvokeMember(name:="FirstName",
invokeAttr:=BindingFlags.SetField,
binder:=Nothing,
target:=obj,
args:={"Francesco"})
Dim value As Object =
type.InvokeMember(name:="FirstName",
invokeAttr:=BindingFlags.GetField,
binder:=Nothing,
target:=obj,
args:=Nothing)
type.InvokeMember(name:="Age",
invokeAttr:=BindingFlags.SetProperty,
binder:=Nothing,
target:=obj,
args:={35S})
type.InvokeMember(name:="SendEmail",
invokeAttr:=BindingFlags.InvokeMethod,
binder:=Nothing,
target:=obj,
args:={"This is a message", 2})
Dim age As Object =
type.InvokeMember(name:="m_Age",
invokeAttr:=BindingFlags.GetField Or
Module InvokeMember
Dim type As Type = Assembly.GetExecutingAssembly().GetType("MyApp.Person")
Dim arguments() As Object = {"Joe", "Doe"}
Dim obj As Object = type.InvokeMember("", BindingFlags.CreateInstance, Nothing, Nothing, arguments)
Sub InvokeMemberMethod()
type.InvokeMember(name:="FirstName",
invokeAttr:=BindingFlags.SetField,
binder:=Nothing,
target:=obj,
args:={"Francesco"})
Dim value As Object =
type.InvokeMember(name:="FirstName",
invokeAttr:=BindingFlags.GetField,
binder:=Nothing,
target:=obj,
args:=Nothing)
type.InvokeMember(name:="Age",
invokeAttr:=BindingFlags.SetProperty,
binder:=Nothing,
target:=obj,
args:={35S})
type.InvokeMember(name:="SendEmail",
invokeAttr:=BindingFlags.InvokeMethod,
binder:=Nothing,
target:=obj,
args:={"This is a message", 2})
Dim age As Object =
type.InvokeMember(name:="m_Age",
invokeAttr:=BindingFlags.GetField Or
BindingFlags.NonPublic Or BindingFlags.Instance,
binder:=Nothing,
target:=obj,
args:=Nothing)
type.InvokeMember(name:="IncrementValue",
invokeAttr:=BindingFlags.InvokeMethod,
binder:=Nothing,
target:=obj,
args:={10})
Console.WriteLine("New value for argument = {0}", {10}(0))
End Sub
End Module
binder:=Nothing,
target:=obj,
args:=Nothing)
type.InvokeMember(name:="IncrementValue",
invokeAttr:=BindingFlags.InvokeMethod,
binder:=Nothing,
target:=obj,
args:={10})
Console.WriteLine("New value for argument = {0}", {10}(0))
End Sub
End Module
当调用一个静态成员时:
代码
Dim count As Object =
type.InvokeMember(name:="Count",
invokeAttr:=BindingFlags.Static Or BindingFlags.GetProperty,
binder:=Nothing,
target:=Nothing,
args:=Nothing)
type.InvokeMember(name:="Count",
invokeAttr:=BindingFlags.Static Or BindingFlags.GetProperty,
binder:=Nothing,
target:=Nothing,
args:=Nothing)
当只是使用某个方法时请使用单数的Get方法,并尽量为查找函数提供更多的信息:
代码
Dim mi As MethodInfo = GetType(String).
GetMethod(name:="IndexOf",
bindingAttr:=BindingFlags.ExactBinding Or
BindingFlags.Public Or
BindingFlags.Instance,
binder:=Nothing,
types:={GetType(Char), GetType(Short)},
modifiers:=Nothing)
GetMethod(name:="IndexOf",
bindingAttr:=BindingFlags.ExactBinding Or
BindingFlags.Public Or
BindingFlags.Instance,
binder:=Nothing,
types:={GetType(Char), GetType(Short)},
modifiers:=Nothing)