PropertyInfo.GetValue メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したオブジェクトのプロパティ値を返します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetValue(Object) |
指定したオブジェクトのプロパティ値を返します。 |
| GetValue(Object, Object[]) |
インデックス付きプロパティのオプションのインデックス値を持つ指定したオブジェクトのプロパティ値を返します。 |
| GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) |
派生クラスでオーバーライドされると、指定したバインディング、インデックス、カルチャ固有の情報を持つ指定したオブジェクトのプロパティ値を返します。 |
GetValue(Object)
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
指定したオブジェクトのプロパティ値を返します。
public:
System::Object ^ GetValue(System::Object ^ obj);
public object GetValue(object obj);
public object? GetValue(object? obj);
member this.GetValue : obj -> obj
Public Function GetValue (obj As Object) As Object
パラメーター
- obj
- Object
プロパティ値が返されるオブジェクト。
返品
指定したオブジェクトのプロパティ値。
例
次の例では、2 つのプロパティを持つ Planet クラスを定義します。 Name、惑星の名前、 Distance、地球からの距離です。 この例では、惑星木星を表すPlanet オブジェクトをインスタンス化し、プロパティに関する情報を表示し、GetValue メソッドを使用して各Planet プロパティの値を取得するGetPropertyValues メソッドに渡します。
using System;
using System.Reflection;
public class Planet
{
private String planetName;
private Double distanceFromEarth;
public Planet(String name, Double distance)
{
planetName = name;
distanceFromEarth = distance;
}
public String Name
{ get { return planetName; } }
public Double Distance
{ get { return distanceFromEarth; }
set { distanceFromEarth = value; } }
}
public class Example
{
public static void Main()
{
Planet jupiter = new Planet("Jupiter", 3.65e08);
GetPropertyValues(jupiter);
}
private static void GetPropertyValues(Object obj)
{
Type t = obj.GetType();
Console.WriteLine("Type is: {0}", t.Name);
PropertyInfo[] props = t.GetProperties();
Console.WriteLine("Properties (N = {0}):",
props.Length);
foreach (var prop in props)
if (prop.GetIndexParameters().Length == 0)
Console.WriteLine(" {0} ({1}): {2}", prop.Name,
prop.PropertyType.Name,
prop.GetValue(obj));
else
Console.WriteLine(" {0} ({1}): <Indexed>", prop.Name,
prop.PropertyType.Name);
}
}
// The example displays the following output:
// Type is: Planet
// Properties (N = 2):
// Name (String): Jupiter
// Distance (Double): 365000000
Imports System.Reflection
Public Class Planet
Private planetName As String
Private distanceFromEarth As Double
Public Sub New(name As String, distance As Double)
planetName = name
distanceFromEarth = distance
End Sub
Public ReadOnly Property Name As String
Get
Return planetName
End Get
End Property
Public Property Distance As Double
Get
Return distanceFromEarth
End Get
Set
distanceFromEarth = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim jupiter As New Planet("Jupiter", 3.65e08)
GetPropertyValues(jupiter)
End Sub
Private Sub GetPropertyValues(obj As Object)
Dim t As Type = obj.GetType()
Console.WriteLine("Type is: {0}", t.Name)
Dim props() As PropertyInfo = t.GetProperties()
Console.WriteLine("Properties (N = {0}):",
props.Length)
For Each prop In props
If prop.GetIndexParameters().Length = 0 Then
Console.WriteLine(" {0} ({1}): {2}", prop.Name,
prop.PropertyType.Name,
prop.GetValue(obj))
Else
Console.WriteLine(" {0} ({1}): <Indexed>", prop.Name,
prop.PropertyType.Name)
End If
Next
End Sub
End Module
' The example displays the following output:
' Type is: Planet
' Properties (N = 2):
' Name (String): Jupiter
' Distance (Double): 365000000
注釈
インデックスのないプロパティの値を取得するには、 GetValue(Object) オーバーロードを呼び出します。インデックス付きプロパティの値を取得しようとすると、メソッドは TargetParameterCountException 例外をスローします。 GetIndexParameters メソッドを呼び出すことで、プロパティがインデックス付けされているかどうかを判断できます。 返される ParameterInfo 配列の長さが 0 の場合、プロパティのインデックスは作成されません。
これは、BindingFlags パラメーターを BindingFlags.Default に設定し、Binderを null に設定し、インデックス値のオブジェクト配列を null に設定し、CultureInfoを null に設定した抽象GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) メソッドの実装を提供する便利なメソッドです。
適用対象
GetValue(Object, Object[])
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
インデックス付きプロパティのオプションのインデックス値を持つ指定したオブジェクトのプロパティ値を返します。
public:
virtual System::Object ^ GetValue(System::Object ^ obj, cli::array <System::Object ^> ^ index);
public virtual object GetValue(object obj, object[] index);
public virtual object? GetValue(object? obj, object?[]? index);
abstract member GetValue : obj * obj[] -> obj
override this.GetValue : obj * obj[] -> obj
Public Overridable Function GetValue (obj As Object, index As Object()) As Object
パラメーター
- obj
- Object
プロパティ値が返されるオブジェクト。
- index
- Object[]
インデックス付きプロパティのオプションのインデックス値。 インデックス付きプロパティのインデックスは 0 から始まります。 この値は、インデックスのないプロパティに対して null する必要があります。
返品
指定したオブジェクトのプロパティ値。
実装
例外
オブジェクトがターゲット型と一致しないか、プロパティがインスタンス プロパティですが、 obj が null。
index内のパラメーターの数が、インデックス付きプロパティが受け取るパラメーターの数と一致しません。
クラス内のプライベート メソッドまたは保護されたメソッドに不正にアクセスしようとしました。
プロパティ値の取得中にエラーが発生しました。 たとえば、インデックス付きプロパティに指定されたインデックス値が範囲外です。 InnerException プロパティは、エラーの理由を示します。
例
次の例は、インデックス付きプロパティの値を取得する方法を示しています。 String.Chars[] プロパティは、String クラスの既定のプロパティ (C# のインデクサー) です。
using System;
using System.Reflection;
class Example
{
public static void Main()
{
string test = "abcdefghijklmnopqrstuvwxyz";
// Get a PropertyInfo object representing the Chars property.
PropertyInfo pinfo = typeof(string).GetProperty("Chars");
// Show the first, seventh, and last letters
ShowIndividualCharacters(pinfo, test, 0, 6, test.Length - 1);
// Show the complete string.
Console.Write("The entire string: ");
for (int x = 0; x < test.Length; x++)
{
Console.Write(pinfo.GetValue(test, new Object[] {x}));
}
Console.WriteLine();
}
static void ShowIndividualCharacters(PropertyInfo pinfo,
object value,
params int[] indexes)
{
foreach (var index in indexes)
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, new object[] { index }));
Console.WriteLine();
}
}
// The example displays the following output:
// Character in position 0: 'a'
// Character in position 6: 'g'
// Character in position 25: 'z'
//
// The entire string: abcdefghijklmnopqrstuvwxyz
Imports System.Reflection
Module Example
Sub Main()
Dim test As String = "abcdefghijklmnopqrstuvwxyz"
' Get a PropertyInfo object representing the Chars property.
Dim pinfo As PropertyInfo = GetType(String).GetProperty("Chars")
' Show the first, seventh, and last characters.
ShowIndividualCharacters(pinfo, test, { 0, 6, test.Length - 1 })
' Show the complete string.
Console.Write("The entire string: ")
For x As Integer = 0 To test.Length - 1
Console.Write(pinfo.GetValue(test, { x }))
Next
Console.WriteLine()
End Sub
Sub ShowIndividualCharacters(pinfo As PropertyInfo,
value As Object,
ParamArray indexes() As Integer)
For Each index In indexes
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, { index }))
Next
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' Character in position 0: 'a'
' Character in position 6: 'g'
' Character in position 25: 'z'
'
' The entire string: abcdefghijklmnopqrstuvwxyz
注釈
プロパティにインデックスが付いているかどうかを確認するには、 GetIndexParameters メソッドを使用します。 結果の配列に 0 (ゼロ) の要素がある場合、プロパティのインデックスは作成されません。
これは、DefaultのBindingFlags パラメーター、Binderを null に設定し、CultureInfoを null に設定した抽象GetValue メソッドの実装を提供する便利なメソッドです。
静的プロパティは個々のオブジェクトではなく型に属しているため、 null をオブジェクト引数として渡すことによって静的プロパティを取得します。 たとえば、次のコードを使用して、CultureInfoの静的CurrentCulture プロパティを取得します。
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
GetValue メソッドを使用するには、最初にクラス Typeを取得します。
Typeから、PropertyInfoを取得します。
PropertyInfoから、GetValue メソッドを使用します。
注
このメソッドを使用すると、呼び出し元が ReflectionPermission フラグを持つReflectionPermissionFlag.RestrictedMemberAccessを許可されている場合、および非パブリック メンバーの許可セットが呼び出し元の許可セットまたはそのサブセットに制限されている場合に、非パブリック メンバーにアクセスできます。 (「リフレクションのセキュリティに関する考慮事項」を参照してください>)。この機能を使用するには、アプリケーションで Framework 3.5 以降.NETターゲットにする必要があります。
適用対象
GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
- ソース:
- PropertyInfo.cs
派生クラスでオーバーライドされると、指定したバインディング、インデックス、カルチャ固有の情報を持つ指定したオブジェクトのプロパティ値を返します。
public:
abstract System::Object ^ GetValue(System::Object ^ obj, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ index, System::Globalization::CultureInfo ^ culture);
public abstract object? GetValue(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? index, System.Globalization.CultureInfo? culture);
public abstract object GetValue(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] index, System.Globalization.CultureInfo culture);
abstract member GetValue : obj * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public MustOverride Function GetValue (obj As Object, invokeAttr As BindingFlags, binder As Binder, index As Object(), culture As CultureInfo) As Object
パラメーター
- obj
- Object
プロパティ値が返されるオブジェクト。
- invokeAttr
- BindingFlags
呼び出し属性を指定する次の列挙メンバーのビットごとの組み合わせ: InvokeMethod、 CreateInstance、 Static、 GetField、 SetField、 GetProperty、および SetProperty。 適切な呼び出し属性を指定する必要があります。 たとえば、静的メンバーを呼び出すには、 Static フラグを設定します。
- binder
- Binder
バインディング、引数型の強制変換、メンバーの呼び出し、リフレクションによる MemberInfo オブジェクトの取得を可能にするオブジェクト。
binderがnullされている場合は、既定のバインダーが使用されます。
- index
- Object[]
インデックス付きプロパティのオプションのインデックス値。 この値は、インデックスのないプロパティに対して null する必要があります。
- culture
- CultureInfo
リソースをローカライズするカルチャ。 リソースがこのカルチャ用にローカライズされていない場合は、一致を検索するために Parent プロパティが連続して呼び出されます。 この値が null場合、カルチャ固有の情報は CurrentUICulture プロパティから取得されます。
返品
指定したオブジェクトのプロパティ値。
実装
例外
オブジェクトがターゲット型と一致しないか、プロパティがインスタンス プロパティですが、 obj が null。
index内のパラメーターの数が、インデックス付きプロパティが受け取るパラメーターの数と一致しません。
クラス内のプライベート メソッドまたは保護されたメソッドに不正にアクセスしようとしました。
プロパティ値の取得中にエラーが発生しました。 たとえば、インデックス付きプロパティに指定されたインデックス値が範囲外です。 InnerException プロパティは、エラーの理由を示します。
注釈
プロパティにインデックスが付いているかどうかを確認するには、 GetIndexParameters メソッドを使用します。 結果の配列に 0 (ゼロ) の要素がある場合、プロパティのインデックスは作成されません。
静的プロパティは個々のオブジェクトではなく型に属しているため、 null をオブジェクト引数として渡すことによって静的プロパティを取得します。 たとえば、次のコードを使用して、CultureInfoの静的CurrentCulture プロパティを取得します。
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
GetValue メソッドを使用するには、最初にクラス Typeを取得します。
Typeから、PropertyInfoを取得します。
PropertyInfoから、GetValue メソッドを使用します。
注
このメソッドを使用すると、呼び出し元が ReflectionPermission フラグを持つReflectionPermissionFlag.RestrictedMemberAccessを許可されている場合、および非パブリック メンバーの許可セットが呼び出し元の許可セットまたはそのサブセットに制限されている場合に、非パブリック メンバーにアクセスできます。 (「リフレクションのセキュリティに関する考慮事項」を参照してください>)。この機能を使用するには、アプリケーションで Framework 3.5 以降.NETターゲットにする必要があります。