Type.GetProperties メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Typeのプロパティを取得します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetProperties() |
現在の Typeのすべてのパブリック プロパティを返します。 |
| GetProperties(BindingFlags) |
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeのプロパティを検索します。 |
GetProperties()
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
現在の Typeのすべてのパブリック プロパティを返します。
public:
cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo[] GetProperties();
public System.Reflection.PropertyInfo[] GetProperties();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
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のすべてのパブリック プロパティを表すType オブジェクトの配列。
-又は-
現在のPropertyInfoにパブリック プロパティがない場合は、Type型の空の配列。
実装
- 属性
例
GetProperties メソッドの使用例を次に示します。
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());
}
// Get the properties of 'Type' class object.
let myPropertyInfo = Type.GetType("System.Type").GetProperties()
printfn "Properties of System.Type are:"
for pi in myPropertyInfo do
printfn $"{pi}"
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
注釈
このオーバーロードの呼び出しは、C# では GetProperties(BindingFlags)、Visual Basic では bindingAttr と等しい BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public 引数を使用して bindingAttr オーバーロードを呼び出すことと同じです。 現在の Type オブジェクトで表される型によって定義されたプロパティと、基本型から継承されたプロパティの両方で、すべてのパブリック インスタンスと静的プロパティが返されます。
プロパティに少なくとも 1 つのパブリック アクセサーがある場合、そのプロパティはリフレクションに対してパブリックであると見なされます。 それ以外の場合、プロパティはプライベートと見なされ、 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic では、 Orを使用して値を結合) を使用して取得する必要があります。
.NET 6 以前のバージョンでは、GetProperties メソッドは、アルファベット順や宣言順序などの特定の順序でプロパティを返しません。 コードは、プロパティが返される順序に依存してはなりません。順序は異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
次の表は、型に反映するときに、 Get メソッドによって返される基底クラスのメンバーを示しています。
| メンバーの種類 | Static | 非静的 |
|---|---|---|
| コンストラクター | いいえ | いいえ |
| フィールド | いいえ | Yes. フィールドは常に名前と署名によって非表示になります。 |
| イベント | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
| Method | いいえ | Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。 |
| 入れ子にされた型 | いいえ | いいえ |
| 財産 | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。
リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。
カスタム属性は、共通型システムの一部ではありません。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ PropertyInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のプロパティを検索します。
こちらもご覧ください
適用対象
GetProperties(BindingFlags)
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeのプロパティを検索します。
public:
abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()
パラメーター
返品
指定したバインディング制約に一致する現在の Type のすべてのプロパティを表すオブジェクトの配列。
-又は-
現在のPropertyInfoにプロパティがない場合、またはバインド制約に一致するプロパティがない場合は、Type型の空の配列。
実装
- 属性
例
次の例では、6 つのプロパティを含む PropertyClass という名前のクラスを定義します。2 つはパブリック、1 つはプライベート、1 つは保護され、1 つは内部 (Visual Basicでは Friend)、1 つは内部 (Visual Basicの Protected Friend) で保護されています。 その後、指定したバインド制約に一致するプロパティの基本的なプロパティ情報 (プロパティの名前と型、読み取り/書き込みかどうか、 get アクセサーと set アクセサーの可視性) が表示されます。
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
open System.Reflection
// Create a class having four properties.
type PropertyClass() =
member _.Property1 =
"hello"
member _.Property2 =
"hello"
member private _.Property3 =
32
member internal _.Property4 =
"value"
let getVisibility (accessor: MethodInfo) =
if accessor.IsPublic then
"Public"
elif accessor.IsPrivate then
"Private"
elif accessor.IsFamily then
"Protected"
elif accessor.IsAssembly then
"Internal/Friend"
else
"Protected Internal/Friend"
let displayPropertyInfo (propInfos: #seq<PropertyInfo>) =
// Display information for all properties.
for propInfo in propInfos do
let readable = propInfo.CanRead
let writable = propInfo.CanWrite
printfn $" Property name: {propInfo.Name}"
printfn $" Property type: {propInfo.PropertyType}"
printfn $" Read-Write: {readable && writable}"
if readable then
let getAccessor = propInfo.GetMethod
printfn $" Visibility: {getVisibility getAccessor}"
if writable then
let setAccessor = propInfo.SetMethod
printfn $" Visibility: {getVisibility setAccessor}"
printfn ""
let t = typeof<PropertyClass>
// Get the public properties.
let propInfos = t.GetProperties(BindingFlags.Public ||| BindingFlags.Instance)
printfn $"The number of public properties: {propInfos.Length}.\n"
// Display the public properties.
displayPropertyInfo propInfos
// Get the nonpublic properties.
let propInfos1 = t.GetProperties(BindingFlags.NonPublic ||| BindingFlags.Instance)
printfn $"The number of non-public properties: {propInfos1.Length}.\n"
// Display all the nonpublic properties.
displayPropertyInfo propInfos1
// 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: 2.
//
// Property name: Property3
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property4
// Property type: System.String
// Read-Write: False
// Visibility: 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
注釈
GetProperties(BindingFlags)オーバーロードがプロパティ情報を正常に取得するには、bindingAttr引数に少なくとも 1 つのBindingFlags.InstanceとBindingFlags.Staticと、少なくとも 1 つのBindingFlags.NonPublicとBindingFlags.Publicが含まれている必要があります。
次の BindingFlags フィルター フラグを使用して、検索に含めるプロパティを定義できます。
インスタンス メソッドを含める
BindingFlags.Instanceを指定します。静的メソッドを含める
BindingFlags.Staticを指定します。検索にパブリック プロパティを含める
BindingFlags.Publicを指定します。 プロパティに少なくとも 1 つのパブリック アクセサーがある場合、そのプロパティはリフレクションに対してパブリックであると見なされます。BindingFlags.NonPublicを指定して、非パブリック プロパティ (プライベート、内部、および保護されたプロパティ) を検索に含めます。 基底クラスの保護されたプロパティと内部プロパティのみが返されます。基底クラスのプライベート プロパティは返されません。BindingFlags.FlattenHierarchyおよびpublic静的メンバーを階層に含めるprotectedを指定します。継承されたクラスprivate静的メンバーは含まれません。空
BindingFlags.DefaultPropertyInfo配列を返すには、単独で指定します。
次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。
-
BindingFlags.DeclaredOnlyは、単に継承されたプロパティではなく、 Typeで宣言されたプロパティのみを検索します。
詳細については、System.Reflection.BindingFlags を参照してください。
.NET 6 以前のバージョンでは、GetProperties メソッドは、アルファベット順や宣言順序などの特定の順序でプロパティを返しません。 コードは、プロパティが返される順序に依存してはなりません。順序は異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ PropertyInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のプロパティを検索します。
こちらもご覧ください
- PropertyInfo
- BindingFlags
- DefaultBinder
- GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])