Export (0) Print
Expand All

PropertyInfo.PropertyType Property

Gets the type of this property.

Namespace:  System.Reflection
Assemblies:   mscorlib (in mscorlib.dll)
  System.Reflection (in System.Reflection.dll)

public abstract Type PropertyType { get; }

Property Value

Type: System.Type
The type of this property.

Implements

_PropertyInfo.PropertyType

To determine the type of a particular property, do the following:

  1. Get a Type object that represents the type (the class or structure) that contains the property. If you are working with an object (an instance of a type), you can call its GetType method. Otherwise, you can use the C# typeof operator or the Visual Basic GetType operator, as the example illustrates.

  2. Get a PropertyInfo object that represents the property in which you're interested. You can do this by getting an array of all properties from the Type.GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type.GetProperty method and specifying the property name.

  3. Retrieve the value of the PropertyType property from the PropertyInfo object.

The following example defines an Employee class that has five properties. It then uses retrieves an array of PropertyInfo objects that represent those properties and displays the name and type of each.

Imports System.Reflection

Public Class Employee
   Private _id As String 

   Public Property FirstName As String = String.Empty
   Public Property MiddleName As String = String.Empty
   Public Property LastName As String = String.Empty
   Public Property HireDate As Date = Date.Today

   Public Property ID As String 
      Get 
         Return _id
      End Get 
      Set 
         If ID.Trim().Length <> 9 Then _
            Throw New ArgumentException("The ID is invalid")
         _id = value
      End Set 
   End Property 
End Class 

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Employee)
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name)
      For Each prop In t.GetProperties()
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name)
      Next 
   End Sub 
End Module 
' The example displays the following output: 
'    The Employee type has the following properties: 
'       FirstName (String) 
'       MiddleName (String) 
'       LastName (String) 
'       HireDate (DateTime) 
'       ID (String)

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft