VB.Net by API/System/Type
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Type.FindMembers
Imports System
Imports System.Reflection
Public Class MainClass
Shared Sub Main( )
Dim t As Type = Type.GetType("System.Reflection.Assembly")
Dim mbrInfoArray As MemberInfo( ) = t.FindMembers( _
MemberTypes.Method, _
BindingFlags.Public Or _
BindingFlags.Static Or _
BindingFlags.NonPublic Or _
BindingFlags.Instance Or _
BindingFlags.DeclaredOnly, _
Type.FilterName, "Get*")
Dim inf As MemberInfo
For Each inf In mbrInfoArray
Console.WriteLine("{0} is a {1}", _
inf, inf.MemberType)
Next
End Sub
End Class
Type.GetHashCode()
Imports System
public class MainClass
Shared Sub Main()
Dim i As Integer = 15
Console.WriteLine(i.ToString())
Console.WriteLine("Hash is: " + i.GetHashCode().ToString())
Console.WriteLine("Type is: " + i.GetType().ToString)
Console.WriteLine("Type full name is: " + i.GetType().FullName())
Console.WriteLine("Type assembly qualified name is: " + i.GetType().AssemblyQualifiedName)
Console.WriteLine("Namespace is: " + i.GetType().Namespace)
End Sub
End Class
Type.GetInterfaces()
Imports System.Reflection
Public Class MainClass
Public Shared Sub Main()
Dim Book = New Derived()
Dim Member As MemberInfo
Console.WriteLine("Members:")
For Each Member In Book.GetType.GetMembers()
Console.WriteLine(Member.Name & " " & Member.MemberType)
Next
Dim PropertyObj As PropertyInfo
Console.WriteLine("Properties:")
For Each PropertyObj In Book.GetType.GetProperties()
Console.WriteLine(PropertyObj.Name & " " & PropertyObj.PropertyType.ToString())
Next
Dim MethodObj As MethodInfo
Console.WriteLine("Methods:")
For Each MethodObj In Book.GetType.GetMethods()
Console.WriteLine(MethodObj.Name & " " & MethodObj.ReturnType.ToString())
Next
Dim EventObj As EventInfo
Console.WriteLine("Events:")
For Each EventObj In Book.GetType.GetEvents()
Console.WriteLine(EventObj.Name & " " & EventObj.IsMulticast)
Next
Dim InterfaceObj As Type
Console.WriteLine("Events:")
For Each InterfaceObj In Book.GetType.GetInterfaces()
Console.WriteLine(InterfaceObj.Name)
Next
End Sub
End Class
Class Base
Public ProductID As String
Public Weight As Double
Private ProductPrice As Double
Public Sub New()
End Sub
Public ReadOnly Property Price() As Double
Get
Return 0
End Get
End Property
End Class
Class Derived
Inherits Base
Implements IFormattable
Public Title As String
Public Author As String
Public Publisher As String
Public Overridable Overloads Function ToString(ByVal _
Format As String, ByVal Provider As IFormatProvider) _
As String Implements IFormattable.ToString
ToString = Title
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
Type.GetMembers()
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("Member Names")
Dim Member As Reflection.MemberInfo
For Each Member In NetBook.GetType.GetMembers()
Console.WriteLine(Member.Name)
Next
End Sub
End Module
Type.GetMethod
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
Type.GetType()
Imports System
public class MainClass
Shared Sub Main()
Dim i As Integer = 15
Console.WriteLine(i.ToString())
Console.WriteLine("Hash is: " + i.GetHashCode().ToString())
Console.WriteLine("Type is: " + i.GetType().ToString)
Console.WriteLine("Type full name is: " + i.GetType().FullName())
Console.WriteLine("Type assembly qualified name is: " + i.GetType().AssemblyQualifiedName)
Console.WriteLine("Namespace is: " + i.GetType().Namespace)
End Sub
End Class
Type.UnderlyingSystemType
Imports System.Reflection
Public Class MainClass
Public Shared Sub Main()
Dim A As Integer
Dim B As Double
Dim C As String = "Hello, world"
Dim D As DateTime = New DateTime(Now.Ticks)
Dim E As Byte
Dim F As Boolean
Console.WriteLine("Type Codes:")
Console.WriteLine("Integer: " & A.GetTypeCode() & _
" " & A.GetType().Name & " " & A.GetType().UnderlyingSystemType.ToString())
Console.WriteLine("Double: " & B.GetTypeCode() & _
" " & B.GetType().Name & " " & B.GetType().UnderlyingSystemType.ToString())
Console.WriteLine("String: " & C.GetTypeCode() & _
" " & C.GetType().Name & " " & C.GetType().UnderlyingSystemType.ToString())
Console.WriteLine("DateTime: " & D.GetTypeCode() & _
" " & D.GetType().Name & " " & D.GetType().UnderlyingSystemType.ToString())
Console.WriteLine("Byte: " & E.GetTypeCode() & _
" " & E.GetType().Name & " " & E.GetType().UnderlyingSystemType.ToString())
Console.WriteLine("Boolean: " & F.GetTypeCode() & _
" " & F.GetType().Name & " " & F.GetType().UnderlyingSystemType.ToString())
End Sub
End Class