本文整理匯總了VB.NET中System.Type.GetMethod方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Type.GetMethod方法的具體用法?VB.NET Type.GetMethod怎麽用?VB.NET Type.GetMethod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Type
的用法示例。
在下文中一共展示了Type.GetMethod方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Program
' 導入命名空間
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an integer and an out parameter. Note that an
' Imports reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(ByVal i As Integer, ByVal l As Long)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32), _
GetType(System.Int64)}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i() As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32())}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
示例2: Program
' 導入命名空間
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an out parameter. Note that an Imports
' reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(ByVal i As Integer, ByVal l As Long)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32), _
GetType(System.Int64)}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i() As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32())}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
示例3: Program
' 導入命名空間
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an out parameter. Note that an Imports
' reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(i As Integer i, l As Long)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(i As Integer())
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(i As Integer, ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _
GetType(Integer).MakeByRefType})
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
示例4: Example
' 導入命名空間
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection
Module Example
Public Sub Main()
' Get a Type object that represents a non-generic type.
GetAddMethod(GetType(ArrayList))
Dim list As New List(Of String)()
' Get a Type object that represents a constructed generic type.
Dim closed As Type = list.GetType()
GetAddMethod(closed)
' Get a Type object that represents an open generic type.
Dim open As Type = GetType(List(Of))
GetAddMethod(open)
End Sub
Private Sub GetAddMethod(typ As Type)
Dim method As MethodInfo
' Determine if this is a generic type.
If typ.IsGenericType Then
' Is it an open generic type?
If typ.ContainsGenericParameters Then
method = typ.GetMethod("Add", typ.GetGenericArguments())
' Get closed generic type arguments.
Else
method = typ.GetMethod("Add", typ.GenericTypeArguments)
End If
' This is not a generic type.
Else
method = typ.GetMethod("Add", { GetType(Object) } )
End If
' Test if an Add method was found.
If method Is Nothing Then
Console.WriteLine("No Add method found.")
Exit Sub
End If
Dim t As Type = method.ReflectedType
Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name)
Dim params() As ParameterInfo = method.GetParameters()
For ctr As Integer = 0 To params.Length - 1
Console.Write("{0}{1}", params(ctr).ParameterType.Name,
If(ctr < params.Length - 1, ", ", ""))
Next
Console.WriteLine(")")
End Sub
End Module
輸出:
System.Collections.ArrayList.Add(Object) System.Collections.Generic.List`1.Add(String) System.Collections.Generic.List`1.Add(T)
示例5: Program
' 導入命名空間
Imports System.Reflection
Class Program
' Method to get:
Public Sub MethodA()
End Sub
Public Shared Sub Main(ByVal args() As String)
' Get MethodA()
Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
示例6: TestClass
' 導入命名空間
Imports System.Reflection
Public Class TestClass
Public Sub DisplayValue(s As String)
Console.WriteLine(s)
End Sub
Public Sub DisplayValue(s As String, ParamArray values() As Object)
Console.WriteLine(s, values)
End Sub
Public Overloads Shared Function Equals(t1 As TestClass, t2 As TestClass) As Boolean
Return Object.ReferenceEquals(t1, t2)
End Function
Public Overloads Function Equals(t As TestClass) As Boolean
Return Object.ReferenceEquals(Me, t)
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(TestClass)
RetrieveMethod(t, "DisplayValue", BindingFlags.Public Or BindingFlags.Instance)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Static)
End Sub
Public Sub RetrieveMethod(t As Type, name As String, flags As BindingFlags)
Try
Dim m As MethodInfo = t.GetMethod(name, flags)
If m IsNot Nothing Then
Console.Write("{0}.{1}(", t.Name, m.Name)
Dim parms() As ParameterInfo = m.GetParameters()
For ctr As Integer = 0 To parms.Length - 1
Console.Write(parms(ctr).ParameterType.Name)
if ctr < parms.Length - 1 Then
Console.Write(", ")
End If
Next
Console.WriteLine(")")
Else
Console.WriteLine("Method not found")
End If
Catch e As AmbiguousMatchException
Console.WriteLine("The following duplicate matches were found:")
Dim methods() As MethodInfo = t.GetMethods(flags)
For Each method In methods
If method.Name <> name Then Continue For
Console.Write(" {0}.{1}(", t.Name, method.Name)
Dim parms() As ParameterInfo = method.GetParameters()
For ctr As Integer = 0 To parms.Length - 1
Console.Write(parms(ctr).ParameterType.Name)
if ctr < parms.Length - 1 Then
Console.Write(", ")
End If
Next
Console.WriteLine(")")
Next
End Try
Console.WriteLine()
End Sub
End Module
輸出:
The following duplicate matches were found: TestClass.DisplayValue(String) TestClass.DisplayValue(String, Object[]) The following duplicate matches were found: TestClass.Equals(TestClass) TestClass.Equals(Object) TestClass.Equals(TestClass) TestClass.Equals(TestClass, TestClass)
示例7: Program
' 導入命名空間
Imports System.Reflection
Class Program
' Method to get:
Public Sub MethodA()
End Sub
Public Shared Sub Main(ByVal args() As String)
' Get MethodA()
Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA")
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
示例8: Person
' 導入命名空間
Imports System.Reflection
Public Class Person
Public FirstName As String
Public LastName As String
Public Overrides Function ToString() As String
Return (FirstName + " " + LastName).Trim()
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(Person)
RetrieveMethod(t, "ToString")
t = GetType(Int32)
RetrieveMethod(t, "ToString")
End Sub
Private Sub RetrieveMethod(t As Type, name As String)
Try
Dim m As MethodInfo = t.GetMethod(name)
If m IsNot Nothing Then
Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
m.Name, If(m.IsStatic, "Static", "Instance"))
Else
Console.WriteLine("{0}.ToString method not found", t.Name)
End If
Catch e As AmbiguousMatchException
Console.WriteLine("{0}.{1} has multiple public overloads.",
t.Name, name)
End Try
End Sub
End Module
輸出:
Person.ToString: Instance method Int32.ToString has multiple public overloads.
示例9: MainClass
' 導入命名空間
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