PropertyInfo.PropertyType Proprietà

Definizione

Ottiene il tipo della proprietà.

public:
 abstract property Type ^ PropertyType { Type ^ get(); };
public abstract Type PropertyType { get; }
member this.PropertyType : Type
Public MustOverride ReadOnly Property PropertyType As Type

Valore della proprietà

Type

Tipo della proprietà.

Implementazioni

Esempio

Nell'esempio seguente viene definita Employee una classe con cinque proprietà. Usa quindi recupera una matrice di oggetti che rappresentano tali proprietà e PropertyInfo visualizza il nome e il tipo di ognuna.

using System;
using System.Reflection;

public class Employee
{
   private string _id;

   public String FirstName { get; set; }
   public String MiddleName { get; set; }
   public String LastName  { get; set; }
   public DateTime HireDate  { get; set; }

   public String ID
   {
      get { return _id; }
      set {
         if (ID.Trim().Length != 9)
            throw new ArgumentException("The ID is invalid");
         _id = value;
      }
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(Employee);
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name);
      foreach (var prop in t.GetProperties())
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name);
   }
}
// The example displays the following output:
//       The Employee type has the following properties:
//          FirstName (String)
//          MiddleName (String)
//          LastName (String)
//          HireDate (DateTime)
//          ID (String)
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)

Commenti

Per determinare il tipo di una determinata proprietà, eseguire le operazioni seguenti:

  1. Ottiene un Type oggetto che rappresenta il tipo (classe o struttura) che contiene la proprietà. Se si utilizza un oggetto (un'istanza di un tipo), è possibile chiamare il relativo GetType metodo . In caso contrario, è possibile usare l'operatore C# o l Visual Basic o operatore GetType, come illustrato nell'esempio.

  2. Ottenere un PropertyInfo oggetto che rappresenta la proprietà a cui si è interessati. A tale scopo, ottenere una matrice di tutte le proprietà dal metodo e quindi eseguire l'iterazione degli elementi nella matrice oppure recuperare l'oggetto che rappresenta la proprietà direttamente chiamando il metodo e specificando il nome della Type.GetProperties PropertyInfo Type.GetProperty proprietà.

  3. Recuperare il valore della PropertyType proprietà PropertyInfo dall'oggetto .

Si applica a