PropertyInfo.PropertyType 속성

정의

이 속성의 형식을 가져옵니다.

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

속성 값

이 속성의 형식입니다.

구현

예제

다음 예제에서는 5개의 Employee 속성이 있는 클래스를 정의합니다. 그런 다음 이러한 속성을 나타내는 개체의 배열을 검색하고 각 개체의 PropertyInfo 이름과 형식을 표시합니다.

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)

설명

특정 속성의 형식을 확인하려면 다음을 수행합니다.

  1. Type 속성을 포함하는 형식(클래스 또는 구조체)을 나타내는 개체를 가져옵니다. 개체(형식의 instance)로 작업하는 경우 해당 메서드를 호출할 GetType 수 있습니다. 그렇지 않으면 예제와 같이 C# 연산자 또는 Visual Basic GetType 연산자를 사용할 수 있습니다.

  2. 관심 있는 PropertyInfo 속성을 나타내는 개체를 가져옵니다. 메서드에서 Type.GetProperties 모든 속성의 배열을 가져오고 배열의 요소를 반복하거나 메서드를 호출 Type.GetProperty 하고 속성 이름을 지정하여 속성을 직접 나타내는 개체를 검색 PropertyInfo 할 수 있습니다.

  3. 개체에서 속성 값을 PropertyType 검색 PropertyInfo 합니다.

적용 대상