Mega Code Archive

 
Categories / VB.Net Tutorial / Class Module
 

Define a class with one property and retrieves the name and type of the property

Imports System Imports System.Reflection Class MyClass1     Private myMessage As [String] = "Hello World."     Public Property MyProperty1() As String         Get             Return myMessage         End Get         Set(ByVal Value As String)             myMessage = Value         End Set     End Property End Class 'MyClass1 Class TestClass     Shared Sub Main()         Try             Dim myType As Type = GetType(MyClass1)             Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))             Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name)             Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType.ToString())         Catch e As ArgumentNullException             Console.WriteLine("ArgumentNullException :" + e.Message.ToString())         Catch e As AmbiguousMatchException             Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())         Catch e As NullReferenceException             Console.WriteLine("Source : {0}", e.Source.ToString())             Console.WriteLine("Message : {0}", e.Message.ToString())         End Try     End Sub End Class