文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>18.3 反射示例

18.3 反射示例

时间:2010-09-04  来源:cuishengli

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

 

 2. 访问成员

 

AccessingMembers 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")
          Console.WriteLine(firstName.GetValue(obj))
    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 
                        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

 

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载