次の方法で共有


Type.GetProperties メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function GetProperties() As PropertyInfo()

[C#] public PropertyInfo[] GetProperties();

[C++] public: PropertyInfo* GetProperties() [];

[JScript] public function GetProperties() : PropertyInfo[];

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public MustOverride Function GetProperties(BindingFlags) As PropertyInfo() Implements IReflect.GetProperties

[C#] public abstract PropertyInfo[] GetProperties(BindingFlags);

[C++] public: virtual PropertyInfo* GetProperties(BindingFlags) [] = 0;

[JScript] public abstract function GetProperties(BindingFlags) : PropertyInfo[];

使用例

[Visual Basic, C#, C++] 2 つのパブリック プロパティと 1 つのプロテクト プロパティを作成し、指定したバインディング制約に一致するプロパティの情報を表示する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、GetProperties のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class having three properties.
Public Class MyTypeClass
    Public ReadOnly Property MyProperty1() As [String]
        Get
            Return "hello"
        End Get
    End Property
    Public ReadOnly Property MyProperty2() As [String]
        Get
            Return "hello"
        End Get
    End Property
    Protected ReadOnly Property MyProperty3() As [String]
        Get
            Return "hello"
        End Get
    End Property
End Class 'MyTypeClass

Public Class TypeMain
    Public Shared Sub Main()
        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public properties.
        Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
        Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length.ToString())
        ' Display the public properties.
        DisplayPropertyInfo(myPropertyInfo)
        ' Get the nonpublic properties.
        Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString())
        ' Display the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1)
    End Sub 'Main

    Public Shared Sub DisplayPropertyInfo(ByVal myPropertyInfo() As PropertyInfo)
        ' Display the information for all properties.
        Dim i As Integer
        For i = 0 To myPropertyInfo.Length - 1
            Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
            Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())
        Next i
    End Sub 'DisplayPropertyInfo
End Class 'TypeMain 

[C#] 

using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class having three properties.
public class MyTypeClass
{
    public String MyProperty1
    {
        get 
        {
            return "hello";
        }
    }
    public String MyProperty2 
    {
        get 
        {
            return "hello";
        }
    }
    protected String MyProperty3
    {
        get
        {
            return "hello";
        }
    }
}

public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public properties.
        PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
        // Display the public properties.
        DisplayPropertyInfo(myPropertyInfo);
        // Get the nonpublic properties.
        PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1);        
    }
    public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
    {
        // Display information for all properties.
        for(int i=0;i<myPropertyInfo.Length;i++)
        {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
            Console.WriteLine("The property name is {0}.", myPropInfo.Name);
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having three properties.
public __gc class MyTypeClass {
public:
   __property String* get_MyProperty1() {
      return S"hello";
   }
   __property String* get_MyProperty2() {
      return S"hello";
   }
protected:
   __property String* get_MyProperty3() {
      return S"hello";
   }
};

void DisplayPropertyInfo(PropertyInfo* myPropertyInfo[]) {
   // Display information for all properties.
   for (int i=0;i<myPropertyInfo->Length;i++) {
      PropertyInfo*  myPropInfo = myPropertyInfo[i];
      Console::WriteLine(S"The property name is {0}.", myPropInfo->Name);
      Console::WriteLine(S"The property type is {0}.", myPropInfo->PropertyType);
   }
}
int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public properties.
   PropertyInfo*  myPropertyInfo[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));
   Console::WriteLine(S"The mumber of public properties is {0}.",__box(  myPropertyInfo->Length));
   // Display the public properties.
   DisplayPropertyInfo(myPropertyInfo);
   // Get the nonpublic properties.
   PropertyInfo*  myPropertyInfo1[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
   Console::WriteLine(S"The number of protected properties is {0}.",__box( myPropertyInfo1->Length));
   // Display all the nonpublic properties.
   DisplayPropertyInfo(myPropertyInfo1);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Type クラス | Type メンバ | System 名前空間