Type.GetProperties メソッド

定義

現在の Type のプロパティを取得します。

オーバーロード

GetProperties()

現在の Type のすべてのパブリック プロパティを返します。

GetProperties(BindingFlags)

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type のプロパティを検索します。

GetProperties()

現在の Type のすべてのパブリック プロパティを返します。

public:
 cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
 virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public System.Reflection.PropertyInfo[] GetProperties ();
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
abstract member GetProperties : unit -> System.Reflection.PropertyInfo[]
override this.GetProperties : unit -> System.Reflection.PropertyInfo[]
Public Function GetProperties () As PropertyInfo()

戻り値

PropertyInfo[]

現在の PropertyInfo のすべてのパブリック プロパティを表す Type オブジェクトの配列。

または 現在の PropertyInfo にパブリック プロパティが存在しない場合は、Type 型の空の配列。

実装

GetProperties メソッドの使用例を次に示します。

array<PropertyInfo^>^myPropertyInfo;

// Get the properties of 'Type' class object.
myPropertyInfo = Type::GetType( "System.Type" )->GetProperties();
Console::WriteLine( "Properties of System.Type are:" );
for ( int i = 0; i < myPropertyInfo->Length; i++ )
{
   Console::WriteLine( myPropertyInfo[ i ] );

}
PropertyInfo[] myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties();
Console.WriteLine("Properties of System.Type are:");
for (int i = 0; i < myPropertyInfo.Length; i++)
{
    Console.WriteLine(myPropertyInfo[i].ToString());
}
Dim myPropertyInfo() As PropertyInfo
' Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties()
Console.WriteLine("Properties of System.Type are:")
Dim i As Integer
For i = 0 To myPropertyInfo.Length - 1
   Console.WriteLine(myPropertyInfo(i).ToString())
Next i

注釈

このオーバーロードを呼び出すのは GetProperties(BindingFlags) 、C# および C# で と等しい引数を持つ オーバーロードを呼び出 bindingAttr BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Visual Basic。 すべてのパブリック インスタンスと静的プロパティが返されます。現在のオブジェクトによって表される型で定義されているプロパティと、その基本型から継承されたプロパティの両方が Type 返されます。

プロパティには、public であるアクセサーが少なくとも 1 つある場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、|| (Visual Basic では、 を使用して値を組み合わせて BindingFlags.NonPublic BindingFlags.Instance 取得する BindingFlags.Static Or 必要があります。

メソッド GetProperties は、アルファベット順や宣言順など、特定の順序でプロパティを返すのではありません。 コードは、プロパティが返される順序に依存する必要はありません。これは、その順序が異なっているからです。

次の表は、メソッドが型に反映するときに返される基本クラスの Get メンバーを示しています。

メンバーの型 静的 非静的
コンストラクター いいえ いいえ
フィールド いいえ はい。 フィールドは常に名前と署名で非表示にされます。
Event 適用なし 一般的な型システム規則は、継承が プロパティを実装するメソッドの継承と同じである点です。 リフレクションは、名前と署名でプロパティを非表示として扱います。 下記の注 2 を参照してください。
メソッド いいえ はい。 メソッド (仮想と非仮想の両方) は、名前で非表示にするか、名前と署名で非表示にできます。
入れ子になった型 いいえ いいえ
プロパティ 適用なし 一般的な型システム規則は、継承が プロパティを実装するメソッドの継承と同じである点です。 リフレクションは、名前と署名でプロパティを非表示として扱います。 下記の注 2 を参照してください。
  1. 名前と署名の非表示では、カスタム修飾子、戻り値の型、パラメーターの型、センチネル、アンマネージ呼び出し規則など、署名のすべての部分が考慮されます。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名で非表示になります。 基本クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしか含めない場合、派生クラスプロパティは基本クラスのプロパティを非表示にし、基本クラスのセッターにアクセスできない。

  3. カスタム属性は、共通型システムの一部ではありません。

現在の が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ オブジェクト Type PropertyInfo を返します。

現在の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約の Type プロパティを検索します。

こちらもご覧ください

適用対象

GetProperties(BindingFlags)

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type のプロパティを検索します。

public:
 abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()

パラメーター

bindingAttr
BindingFlags

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または

空の配列を返す場合は Default

戻り値

PropertyInfo[]

現在の Type のプロパティのうち、指定したバインディング制約に一致するすべてのプロパティを表すオブジェクトの配列。

または 現在の PropertyInfo にプロパティが設定されていないか、またはプロパティの中でバインディング制約に一致するものが存在しない場合は、Type 型の空の配列。

実装

次の例では、6 つのプロパティを含む という名前のクラスを定義します。2 つはパブリック、1 つはプライベート、1 つは保護され、1 つは内部 (Visual Basic)、1 つは保護された内部 PropertyClass (Visual Basic) です。 Friend Protected Friend 次に、指定したバインディング制約に一致するプロパティの基本的なプロパティ情報 (プロパティ名と型、読み取り/書き込みかどうか、および アクセサーとアクセサーの可視性) が表示されます get set

using namespace System;
using namespace System::Reflection;

// Create a class having three properties.
public ref class PropertyClass
{

public:
   property String^ Property1
   {
      String^ get()
      {
         return "hello";
      }
   }

   property String^ Property2 
   {
      String^ get()
      {
         return "hello";
      }
   }

protected:
   property String^ Property3
   {
      String^ get()
      {
         return "hello";
      }
   }

private:
   property int Property4
   {
      int get()
      {
         return 32;
      }
   }

internal:
   property String^ Property5
   {
      String^ get()
      {
         return "value";
      }
   }
   
public protected:
   property String^ Property6
   {
      String^ get()
      {
         return "value";
      }
   }
};

String^ GetVisibility(MethodInfo^ accessor)
{
    if (accessor->IsPublic)
       return "Public";
    else if (accessor->IsPrivate)
       return "Private";
    else if (accessor->IsFamily)
       return "Protected";
    else if (accessor->IsAssembly)
       return "Internal/Friend";
    else
       return "Protected Internal/Friend";
}

void DisplayPropertyInfo(array<PropertyInfo^>^ propInfos )
{
   // Display information for all properties.
   for each(PropertyInfo^ propInfo in propInfos) {
      bool readable = propInfo->CanRead;
      bool writable = propInfo->CanWrite;
      
      Console::WriteLine("   Property name: {0}", propInfo->Name);
      Console::WriteLine("   Property type: {0}", propInfo->PropertyType);
      Console::WriteLine("   Read-Write:    {0}", readable && writable);
      if (readable) {
         MethodInfo^ getAccessor = propInfo->GetMethod;
         Console::WriteLine("   Visibility:    {0}",
                           GetVisibility(getAccessor));
      }
      if (writable) {
         MethodInfo^ setAccessor = propInfo->SetMethod;
         Console::WriteLine("   Visibility:    {0}",
                            GetVisibility(setAccessor));
      }
      Console::WriteLine();
   }
}

void main()
{
   Type^ myType = PropertyClass::typeid;
   
   // Get the public properties.
   array<PropertyInfo^>^propInfos = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
   Console::WriteLine("The number of public properties: {0}.\n",
                      propInfos->Length);
   // Display the public properties.
   DisplayPropertyInfo( propInfos );
   
   // Get the non-public properties.
   array<PropertyInfo^>^propInfos1 = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine("The number of non-public properties: {0}.\n",
                      propInfos1->Length);
   // Display all the non-public properties.
   DisplayPropertyInfo(propInfos1);
}
// The example displays the following output:
//       The number of public properties: 2.
//
//          Property name: Property2
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//          Property name: Property1
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//       The number of non-public properties: 4.
//
//          Property name: Property6
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected Internal/Friend
//
//          Property name: Property5
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Internal/Friend
//
//          Property name: Property4
//          Property type: System.Int32
//          Read-Write:    False
//          Visibility:    Private
//
//          Property name: Property3
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected
using System;
using System.Reflection;

// Create a class having six properties.
public class PropertyClass
{
    public String Property1
    {
        get { return "hello"; }
    }

    public String Property2
    {
        get { return "hello"; }
    }

    protected String Property3
    {
        get { return "hello"; }
    }

    private Int32 Property4
    {
        get { return 32; }
    }

    internal String Property5
    {
       get { return "value"; }
    }

    protected internal String Property6
    {
       get { return "value"; }
    }
}

public class Example
{
    public static void Main()
    {
        Type t = typeof(PropertyClass);
        // Get the public properties.
        PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The number of public properties: {0}.\n",
                          propInfos.Length);
        // Display the public properties.
        DisplayPropertyInfo(propInfos);

        // Get the nonpublic properties.
        PropertyInfo[] propInfos1 = t.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of non-public properties: {0}.\n",
                          propInfos1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(propInfos1);
    }

    public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
    {
        // Display information for all properties.
        foreach (var propInfo in propInfos) {
            bool readable = propInfo.CanRead;
            bool writable = propInfo.CanWrite;

            Console.WriteLine("   Property name: {0}", propInfo.Name);
            Console.WriteLine("   Property type: {0}", propInfo.PropertyType);
            Console.WriteLine("   Read-Write:    {0}", readable & writable);
            if (readable) {
               MethodInfo getAccessor = propInfo.GetMethod;
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(getAccessor));
            }
            if (writable) {
               MethodInfo setAccessor = propInfo.SetMethod;
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(setAccessor));
            }
            Console.WriteLine();
        }
    }

    public static String GetVisibility(MethodInfo accessor)
    {
       if (accessor.IsPublic)
          return "Public";
       else if (accessor.IsPrivate)
          return "Private";
       else if (accessor.IsFamily)
          return "Protected";
       else if (accessor.IsAssembly)
          return "Internal/Friend";
       else
          return "Protected Internal/Friend";
    }
}
// The example displays the following output:
//       The number of public properties: 2.
//
//          Property name: Property1
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//          Property name: Property2
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//       The number of non-public properties: 4.
//
//          Property name: Property3
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected
//
//          Property name: Property4
//          Property type: System.Int32
//          Read-Write:    False
//          Visibility:    Private
//
//          Property name: Property5
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Internal/Friend
//
//          Property name: Property6
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected Internal/Friend
Imports System.Reflection

' Create a class having six properties.
Public Class PropertyClass
    Public ReadOnly Property Property1() As String
        Get
            Return "hello"
        End Get
    End Property

    Public ReadOnly Property Property2() As String
        Get
            Return "hello"
        End Get
    End Property

    Protected ReadOnly Property Property3() As String
        Get
            Return "hello"
        End Get
    End Property

    Private ReadOnly Property Property4 As Integer
        Get
           Return 32
        End Get
    End Property

    Friend ReadOnly Property Property5 As String
       Get
          Return "value"
       End Get
    End Property

    Protected Friend ReadOnly Property Property6 As String
       Get
          Return "value"
       End Get
    End Property
End Class

Public Module Example
    Public Sub Main()
        Dim t As Type = GetType(PropertyClass)
        ' Get the public properties.
        Dim propInfos As PropertyInfo() = t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("The number of public properties: {0}",
                          propInfos.Length)
        Console.WriteLine()
        ' Display the public properties.
        DisplayPropertyInfo(propInfos)

        ' Get the non-public properties.
        Dim propInfos1 As PropertyInfo() = t.GetProperties(BindingFlags.NonPublic Or BindingFlags.Instance)
        Console.WriteLine("The number of non-public properties: {0}",
                          propInfos1.Length)
        Console.WriteLine()
        ' Display all the non-public properties.
        DisplayPropertyInfo(propInfos1)
    End Sub

    Public Sub DisplayPropertyInfo(ByVal propInfos() As PropertyInfo)
        ' Display information for all properties.
        For Each propInfo In propInfos
            Dim readable As Boolean = propInfo.CanRead
            Dim writable As Boolean = propInfo.CanWrite
            
            Console.WriteLine("   Property name: {0}", propInfo.Name)
            Console.WriteLine("   Property type: {0}", propInfo.PropertyType)
            Console.WriteLine("   Read-Write:    {0}", readable And writable)
            If readable Then
               Dim getAccessor As MethodInfo = propInfo.GetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(getAccessor))
            End If
            If writable Then
               Dim setAccessor As MethodInfo = propInfo.SetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(setAccessor))
            End If
            Console.WriteLine()
        Next
    End Sub
    
    Public Function GetVisibility(accessor As MethodInfo) As String
       If accessor.IsPublic Then
          Return "Public"
       ElseIf accessor.IsPrivate Then
          Return "Private"
       Else If accessor.IsFamily Then
          Return "Protected"
       Else If accessor.IsAssembly Then
          Return "Internal/Friend"
       Else
          Return "Protected Internal/Friend"
       End If
    End Function
End Module
' The example displays the following output:
'       The number of public properties: 2
'
'          Property name: Property1
'          Property type: System.String
'          Read-Write:    False
'          Visibility:    Public
'
'          Property name: Property2
'          Property type: System.String
'          Read-Write:    False
'          Visibility:    Public
'
'       The number of non-public properties: 4
'
'          Property name: Property3
'          Property type: System.String
'          Read-Write:    False
'          Visibility:    Protected
'
'          Property name: Property4
'          Property type: System.Int32
'          Read-Write:    False
'          Visibility:    Private
'
'          Property name: Property5
'          Property type: System.String
'          Read-Write:    False
'          Visibility:    Internal/Friend
'
'          Property name: Property6
'          Property type: System.String
'          Read-Write:    False
'          Visibility:    Protected Internal/Friend

注釈

オーバーロードがプロパティ情報を正常に取得するには、引数に と の少なくとも 1 つと、 と の少なくとも 1 つを含 GetProperties(BindingFlags) bindingAttr BindingFlags.Instance BindingFlags.Static める必要 BindingFlags.NonPublic があります BindingFlags.Public

検索に BindingFlags 含めるプロパティを定義するには、次のフィルター フラグを使用できます。

  • インスタンス BindingFlags.Instance メソッドを含めるには、 を指定します。

  • 静的 BindingFlags.Static メソッドを含めるには、 を指定します。

  • 検索 BindingFlags.Public にパブリック プロパティを含めるには、 を指定します。 プロパティには、public であるアクセサーが少なくとも 1 つある場合、リフレクションに対してパブリックと見なされます。

  • 非パブリック プロパティ (つまり、プライベート、内部、および保護されたプロパティ) を検索に含 BindingFlags.NonPublic める場合に指定します。 基本クラスの保護されたプロパティと内部プロパティだけが返されます。基本クラスのプライベート プロパティは返されません。

  • 階層 BindingFlags.FlattenHierarchy のメンバーと静的メンバーを含める場合は を指定します。継承されたクラスの静的 public protected private メンバーは含まれません。

  • BindingFlags.Default の配列を返す場合は、単独で PropertyInfo 指定します。

検索のしくみ BindingFlags を変更するには、次の修飾子フラグを使用できます。

  • BindingFlags.DeclaredOnly 単純に継承されたプロパティではなく、 で宣言された Type プロパティのみを検索する場合は 。

詳細については、「System.Reflection.BindingFlags」を参照してください。

メソッド GetProperties は、アルファベット順や宣言順など、特定の順序でプロパティを返すのではありません。 コードは、プロパティが返される順序に依存する必要はありません。これは、その順序が異なっているからです。

現在の が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ オブジェクト Type PropertyInfo を返します。

現在の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約の Type プロパティを検索します。

こちらもご覧ください

適用対象