VB.Net by API/System.Reflection/MethodInfo

Материал из VB Эксперт
Версия от 12:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

MethodInfo.GetCustomAttributes

  
Imports System.Reflection

Public Class MainClass
   Public Shared Sub Main()
        Dim MethodObj As System.Reflection.MethodInfo
        Dim MessageDemo As New Demo()
        For Each MethodObj In MessageDemo.GetType.GetMethods()
            Dim Attr As Attribute
            For Each Attr In MethodObj.GetCustomAttributes(False)
                Console.WriteLine(MethodObj.Name)
                Console.WriteLine(Attr)
                Console.WriteLine(CType(Attr, UserName).Name)
            Next
        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
    <UserName("Name 1")> Sub DemoMsg()
        Console.WriteLine("Message")
    End Sub
    <UserName("Name 2")> Sub Greet()
        Console.WriteLine("Hello")
    End Sub
End Class


MethodInfo.GetParameters()

  
Imports System.Reflection

Public Class MainClass
   Public Shared Sub Main()
        Dim SomeObj = New Demo()
        Dim IntegerVar As Integer = 1
        Dim DoubleVar As Double = 100.0
        Dim StringVar As String = "Hello"
        Dim Param As ParameterInfo
        Dim MethodObj As System.Reflection.MethodInfo
        For Each MethodObj In SomeObj.GetType.GetMethods()
            Dim Parameters(MethodObj.GetParameters().Length - 1) As Object
            Dim CallMethod As Boolean = True
            Dim I As Integer = 0
            For Each Param In MethodObj.GetParameters()
                If Equals(Param.ParameterType, IntegerVar.GetType()) Then
                    Parameters(I) = IntegerVar
                ElseIf Equals(Param.ParameterType, DoubleVar.GetType()) Then
                    Parameters(I) = DoubleVar
                ElseIf Equals(Param.ParameterType, StringVar.GetType()) Then
                    Parameters(I) = StringVar
                Else
                    CallMethod = False
                End If
                I = I + 1
            Next
            If (CallMethod) Then
                If MethodObj.GetParameters().Length = 0 Then
                    Console.WriteLine("Calling: " & MethodObj.Name)
                    Console.WriteLine(MethodObj.Invoke(SomeObj, Nothing))
                Else
                    Console.WriteLine("Calling: " & MethodObj.Name)
                    Console.WriteLine(MethodObj.Invoke(SomeObj, Parameters))
                End If
            End If
            Console.WriteLine()
        Next
   
   End Sub
End Class 

    Class Demo
        Public Sub Hello()
        End Sub
        Public Sub ShowMessage(ByVal Msg As String)
        End Sub
        Public Function AddTwoIntegers(ByVal A As Integer, ByVal B As Integer) As Integer
        End Function
        Public Sub ShowThreeDoubles(ByVal A As Double, ByVal B As Double, ByVal C As Double)
        End Sub
    End Class


MethodInfo.Invoke

  
Imports System
Imports System.Reflection
 
Public Class MainClass
    Shared Sub Main(  )
         Dim theMathType As Type = Type.GetType("System.Math")
         Dim paramTypes(0) As Type
         paramTypes(0) = Type.GetType("System.Double")
         Dim ConsineInfo As MethodInfo = _
             theMathType.GetMethod("Sin", paramTypes)
         Dim parameters(0) As Object
         parameters(0) = 45 * (Math.PI / 180)
         Dim returnVal As Object
         returnVal = ConsineInfo.Invoke(theMathType, parameters)
         Console.WriteLine("The sine of a 45 degree angle is {0}", _
             returnVal)
    End Sub
  
End Class


MethodInfo.Name

  
Module Module1
    Class Book
        Public Title As String
        Public Author As String
        Public Price As Double
        Public Sub New(ByVal Title As String, ByVal Author As String, ByVal Price As Double)
            Me.Title = Title
            Me.Author = Author
            Me.Price = Price
        End Sub
        Public Sub ShowTitle()
            Console.WriteLine("Title: " & Title)
        End Sub
        Public Sub ShowBook()
            Console.WriteLine("Title: " & Title)
            Console.WriteLine("Author: " & Author)
            Console.WriteLine("Price: " & Price)
        End Sub
    End Class
    Sub Main()
        Dim NetBook As New Book("AAA","BBB", 49.99)
        Console.WriteLine("Method Names")
        Dim Info As Reflection.MethodInfo
        For Each Info In NetBook.GetType.GetMethods()
            Console.WriteLine(Info.Name)
        Next
    End Sub
End Module