PropertyInfo.PropertyType Właściwość

Definicja

Pobiera typ tej właściwości.

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

Wartość właściwości

Typ tej właściwości.

Implementuje

Przykłady

W poniższym przykładzie zdefiniowano klasę Employee , która ma pięć właściwości. Następnie jest używana funkcja pobierania tablicy PropertyInfo obiektów reprezentujących te właściwości i wyświetla nazwę i typ każdego z nich.

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)

Uwagi

Aby określić typ określonej właściwości, wykonaj następujące czynności:

  1. Pobierz obiekt reprezentujący typ (klasę Type lub strukturę), który zawiera właściwość. Jeśli pracujesz z obiektem (wystąpieniem typu), możesz wywołać jego GetType metodę. W przeciwnym razie możesz użyć operatora C# lub operatora GetType języka Visual Basic, jak pokazano w przykładzie.

  2. PropertyInfo Pobierz obiekt reprezentujący właściwość, w której cię interesuje. Można to zrobić, pobierając tablicę wszystkich właściwości z Type.GetProperties metody, a następnie iterując elementy w tablicy, lub możesz pobrać PropertyInfo obiekt reprezentujący właściwość bezpośrednio, wywołując Type.GetProperty metodę i określając nazwę właściwości.

  3. Pobierz wartość PropertyType właściwości z PropertyInfo obiektu.

Dotyczy