PropertyInfo.PropertyType PropertyInfo.PropertyType PropertyInfo.PropertyType PropertyInfo.PropertyType Property

定義

このプロパティの型を取得します。Gets the type of this property.

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

プロパティ値

このプロパティの型。The type of this property.

実装

次の例ではEmployee 、5つのプロパティを持つクラスを定義します。The following example defines an Employee class that has five properties. 次に、を使用してPropertyInfo 、これらのプロパティを表すオブジェクトの配列を取得し、それぞれの名前と型を表示します。It then uses retrieves an array of PropertyInfo objects that represent those properties and displays the name and type of each.

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)

注釈

特定のプロパティの種類を特定するには、次の手順を実行します。To determine the type of a particular property, do the following:

  1. プロパティをType含む型 (クラスまたは構造体) を表すオブジェクトを取得します。Get a Type object that represents the type (the class or structure) that contains the property. オブジェクト (型のインスタンス) を使用している場合は、そのGetTypeメソッドを呼び出すことができます。If you are working with an object (an instance of a type), you can call its GetType method. それ以外の場合は、 C#この例に示すように、演算子または Visual Basic GetType演算子を使用できます。Otherwise, you can use the C# operator or the Visual Basic GetType operator, as the example illustrates.

  2. 興味のPropertyInfoあるプロパティを表すオブジェクトを取得します。Get a PropertyInfo object that represents the property in which you're interested. これを行うには、 Type.GetPropertiesメソッドからすべてのプロパティの配列を取得してから、配列内の要素を反復処理するか、 Type.GetPropertyメソッドを呼び出してプロパティを表すPropertyInfoオブジェクトを直接取得します。プロパティ名。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. オブジェクトからプロパティPropertyTypeの値を取得します。 PropertyInfoRetrieve the value of the PropertyType property from the PropertyInfo object.

適用対象