Type.GenericParameterPosition プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
Type オブジェクトがジェネリック型またはジェネリック メソッドの型パラメーターを表す場合に、パラメーターを宣言したジェネリック型またはジェネリック メソッドの型パラメーター リスト内の型パラメーターの位置を取得します。
public:
abstract property int GenericParameterPosition { int get(); };
public:
virtual property int GenericParameterPosition { int get(); };
public abstract int GenericParameterPosition { get; }
public virtual int GenericParameterPosition { get; }
member this.GenericParameterPosition : int
Public MustOverride ReadOnly Property GenericParameterPosition As Integer
Public Overridable ReadOnly Property GenericParameterPosition As Integer
プロパティ値
パラメーターを定義するジェネリック型またはジェネリック メソッドの型パラメーター リスト内の型パラメーターの位置。 位置の番号は 0 で始まります。
例外
現在の型は型パラメーターを表していません。 つまり、IsGenericParameter は false
を返します。
例
次の例では、2 つの型パラメーターを持つジェネリック クラスを定義し、最初のクラスから派生する 2 番目のジェネリック クラスを定義します。 派生クラスの基本クラスには、2 つの型引数があります。1 つ目は で、2 つ目は派生型 Int32 の型パラメーターです。 この例では、 プロパティによって報告される位置など、これらのジェネリック クラスに関する情報が表示 GenericParameterPosition されます。
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;
// Define a base class with two type parameters.
generic< class T,class U >
public ref class Base {};
// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
// (1) Its generic type definition is Base<T, U>.
// (2) It specifies int for the first type parameter.
// (3) For the second type parameter, it uses the same type that is used
// for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
generic<class V>
public ref class Derived : Base<int,V> {};
public ref class Test
{
public:
static void Main()
{
Console::WriteLine(
L"\r\n--- Display a generic type and the open constructed");
Console::WriteLine(L" type from which it is derived.");
// Create a Type object representing the generic type definition
// for the Derived type. Note the absence of type arguments.
//
Type^ derivedType = Derived::typeid;
DisplayGenericTypeInfo(derivedType);
// Display its open constructed base type.
DisplayGenericTypeInfo(derivedType->BaseType);
}
private:
static void DisplayGenericTypeInfo(Type^ t)
{
Console::WriteLine(L"\r\n{0}", t);
Console::WriteLine(L"\tIs this a generic type definition? {0}",
t->IsGenericTypeDefinition);
Console::WriteLine(L"\tIs it a generic type? {0}", t->IsGenericType);
Console::WriteLine(L"\tDoes it have unassigned generic parameters? {0}",
t->ContainsGenericParameters);
if (t->IsGenericType)
{
// If this is a generic type, display the type arguments.
//
array<Type^>^typeArguments = t->GetGenericArguments();
Console::WriteLine(L"\tList type arguments ({0}):",
typeArguments->Length);
System::Collections::IEnumerator^ myEnum =
typeArguments->GetEnumerator();
while (myEnum->MoveNext())
{
Type^ tParam = safe_cast<Type^>(myEnum->Current);
// IsGenericParameter is true only for generic type
// parameters.
//
if (tParam->IsGenericParameter)
{
Console::WriteLine(
L"\t\t{0} (unassigned - parameter position {1})",
tParam, tParam->GenericParameterPosition);
}
else
{
Console::WriteLine(L"\t\t{0}", tParam);
}
}
}
}
};
int main()
{
Test::Main();
}
/* This example produces the following output:
--- Display a generic type and the open constructed
type from which it is derived.
Derived`1[V]
Is this a generic type definition? True
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (1):
V (unassigned - parameter position 0)
Base`2[System.Int32,V]
Is this a generic type definition? False
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (2):
System.Int32
V (unassigned - parameter position 0)
*/
using System;
using System.Reflection;
using System.Collections.Generic;
// Define a base class with two type parameters.
public class Base<T, U> { }
// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
// (1) Its generic type definition is Base<T, U>.
// (2) It specifies int for the first type parameter.
// (3) For the second type parameter, it uses the same type that is used
// for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
public class Derived<V> : Base<int, V> { }
public class Test
{
public static void Main()
{
Console.WriteLine(
"\r\n--- Display a generic type and the open constructed");
Console.WriteLine(" type from which it is derived.");
// Create a Type object representing the generic type definition
// for the Derived type, by omitting the type argument. (For
// types with multiple type parameters, supply the commas but
// omit the type arguments.)
//
Type derivedType = typeof(Derived<>);
DisplayGenericTypeInfo(derivedType);
// Display its open constructed base type.
DisplayGenericTypeInfo(derivedType.BaseType);
}
private static void DisplayGenericTypeInfo(Type t)
{
Console.WriteLine("\r\n{0}", t);
Console.WriteLine("\tIs this a generic type definition? {0}",
t.IsGenericTypeDefinition);
Console.WriteLine("\tIs it a generic type? {0}",
t.IsGenericType);
Console.WriteLine("\tDoes it have unassigned generic parameters? {0}",
t.ContainsGenericParameters);
if (t.IsGenericType)
{
// If this is a generic type, display the type arguments.
//
Type[] typeArguments = t.GetGenericArguments();
Console.WriteLine("\tList type arguments ({0}):",
typeArguments.Length);
foreach (Type tParam in typeArguments)
{
// IsGenericParameter is true only for generic type
// parameters.
//
if (tParam.IsGenericParameter)
{
Console.WriteLine(
"\t\t{0} (unassigned - parameter position {1})",
tParam,
tParam.GenericParameterPosition);
}
else
{
Console.WriteLine("\t\t{0}", tParam);
}
}
}
}
}
/* This example produces the following output:
--- Display a generic type and the open constructed
type from which it is derived.
Derived`1[V]
Is this a generic type definition? True
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (1):
V (unassigned - parameter position 0)
Base`2[System.Int32,V]
Is this a generic type definition? False
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (2):
System.Int32
V (unassigned - parameter position 0)
*/
Imports System.Reflection
Imports System.Collections.Generic
' Define a base class with two type parameters.
Public Class Base(Of T, U)
End Class
' Define a derived class. The derived class inherits from a constructed
' class that meets the following criteria:
' (1) Its generic type definition is Base<T, U>.
' (2) It uses int for the first type parameter.
' (3) For the second type parameter, it uses the same type that is used
' for the type parameter of the derived class.
' Thus, the derived class is a generic type with one type parameter, but
' its base class is an open constructed type with one assigned type
' parameter and one unassigned type parameter.
Public Class Derived(Of V)
Inherits Base(Of Integer, V)
End Class
Public Class Test
Public Shared Sub Main()
Console.WriteLine(vbCrLf _
& "--- Display a generic type and the open constructed")
Console.WriteLine(" type from which it is derived.")
' Create a Type object representing the generic type definition
' for the Derived type, by omitting the type argument. (For
' types with multiple type parameters, supply the commas but
' omit the type arguments.)
'
Dim derivedType As Type = GetType(Derived(Of ))
DisplayGenericTypeInfo(derivedType)
' Display its open constructed base type.
DisplayGenericTypeInfo(derivedType.BaseType)
End Sub
Private Shared Sub DisplayGenericTypeInfo(ByVal t As Type)
Console.WriteLine(vbCrLf & "{0}", t)
Console.WriteLine(vbTab & "Is this a generic type definition? " _
& t.IsGenericTypeDefinition)
Console.WriteLine(vbTab & "Is it a generic type? " _
& t.IsGenericType)
Console.WriteLine(vbTab _
& "Does it have unassigned generic parameters? " _
& t.ContainsGenericParameters)
If t.IsGenericType Then
' If this is a generic type, display the type arguments.
'
Dim typeArguments As Type() = t.GetGenericArguments()
Console.WriteLine(vbTab & "List type arguments (" _
& typeArguments.Length & "):")
For Each tParam As Type In typeArguments
' IsGenericParameter is true only for generic type
' parameters.
'
If tParam.IsGenericParameter Then
Console.WriteLine(vbTab & vbTab & tParam.ToString() _
& " (unassigned - parameter position " _
& tParam.GenericParameterPosition & ")")
Else
Console.WriteLine(vbTab & vbTab & tParam.ToString())
End If
Next tParam
End If
End Sub
End Class
' This example produces the following output:
'
'--- Display a generic type and the open constructed
' type from which it is derived.
'
'Derived`1[V]
' Is this a generic type definition? True
' Is it a generic type? True
' Does it have unassigned generic parameters? True
' List type arguments (1):
' V (unassigned - parameter position 0)
'
'Base`2[System.Int32,V]
' Is this a generic type definition? False
' Is it a generic type? True
' Does it have unassigned generic parameters? True
' List type parameters (2):
' System.Int32
' V (unassigned - parameter position 0)
'
注釈
プロパティは、型パラメーターが最初に定義されたジェネリック型定義またはジェネリック メソッド定義のパラメーター リスト内の型パラメーターの位置 GenericParameterPosition を返します。 プロパティ DeclaringType と プロパティ DeclaringMethod は、ジェネリック型またはメソッド定義を識別します。
プロパティがジェネリック メソッド定義を表す を返し、現在の オブジェクトが、そのジェネリック メソッド定義の型 DeclaringMethod MethodInfo MethodInfo Type パラメーターを表す場合。
プロパティが を返す場合、プロパティは常にジェネリック型定義を表す オブジェクトを返し、現在のオブジェクトはそのジェネリック型定義の型 DeclaringMethod
null
DeclaringType Type Type パラメーターを表します。
プロパティの値に適切なコンテキストを提供するには、型パラメーターが属するジェネリック型またはメソッドを識別 GenericParameterPosition する必要があります。 たとえば、次のコードのジェネリック メソッドの戻り GetSomething
値を考えます。
generic<typename T, typename U> public ref class B { };
generic<typename V> public ref class A
{
public:
generic<typename X> B<V, X>^ GetSomething()
{
return gcnew B<V, X>();
}
};
public class B<T, U> { }
public class A<V>
{
public B<V, X> GetSomething<X>()
{
return new B<V, X>();
}
}
Public Class B(Of T, U)
End Class
Public Class A(Of V)
Public Function GetSomething(Of X)() As B(Of V, X)
Return New B(Of V, X)()
End Function
End Class
によって返される型は、クラスとそれ自体に指定された型 GetSomething
A
引数によって GetSomething
異なります。 の を取得 MethodInfo し GetSomething
、そこから戻り値の型を取得できます。 戻り値の型パラメーターを調べると、両方 GenericParameterPosition に対して 0 が返されます。 はクラス の型パラメーター リストの最初の型パラメーターなので、 の V
位置は 0 V
です A
。 の位置は 0 です。これは、 の型パラメーター リストの最初 X
X
の型パラメーターです GetSomething
。
注意
プロパティを GenericParameterPosition 呼び出した場合、現在の が型パラメーター Type を表していない場合、例外が発生します。 オープン構築型の型引数を調べる場合は、 プロパティを使用して、型パラメーターと型を IsGenericParameter 指定します。 プロパティは型パラメーターに対して を返します。その後、 メソッドを使用して位置を取得し、 プロパティと プロパティを使用して、それを定義するジェネリック メソッドまたは型定義 IsGenericParameter true
GenericParameterPosition DeclaringMethod DeclaringType を決定できます。