Share via


PropertyInfo.PropertyType Properti

Definisi

Mendapatkan jenis properti ini.

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

Nilai Properti

Jenis properti ini.

Penerapan

Contoh

Contoh berikut mendefinisikan Employee kelas yang memiliki lima properti. Kemudian menggunakan mengambil array PropertyInfo objek yang mewakili properti tersebut dan menampilkan nama dan jenis masing-masing.

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)

Keterangan

Untuk menentukan jenis properti tertentu, lakukan hal berikut:

  1. Type Dapatkan objek yang mewakili jenis (kelas atau struktur) yang berisi properti . Jika Anda bekerja dengan objek (instans jenis), Anda dapat memanggil metodenya GetType . Jika tidak, Anda dapat menggunakan operator C# atau operator GetType Visual Basic, seperti yang diilustrasikan contohnya.

  2. PropertyInfo Dapatkan objek yang mewakili properti tempat Anda tertarik. Anda dapat melakukan ini dengan mendapatkan array semua properti dari Type.GetProperties metode dan kemudian melakukan iterasi elemen dalam array, atau Anda dapat mengambil PropertyInfo objek yang mewakili properti secara langsung dengan memanggil Type.GetProperty metode dan menentukan nama properti.

  3. Ambil nilai PropertyType properti dari PropertyInfo objek .

Berlaku untuk