Type.GenericParameterPosition プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Type.GenericParameterPosition プロパティの意味・解説 

Type.GenericParameterPosition プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

Type オブジェクトジェネリック型またはジェネリック メソッド型パラメータを表す場合に、パラメータ宣言したジェネリック型またはジェネリック メソッド型パラメータ リスト内の型パラメータ位置取得します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable ReadOnly
 Property GenericParameterPosition As Integer
Dim instance As Type
Dim value As Integer

value = instance.GenericParameterPosition
public virtual int GenericParameterPosition
 { get; }
public:
virtual property int GenericParameterPosition {
    int get ();
}
/** @property */
public int get_GenericParameterPosition ()
public function get GenericParameterPosition
 () : int

プロパティ
パラメータ宣言するジェネリック型またはジェネリック メソッド型パラメータ リスト内の型パラメータ位置位置番号は 0 で始まります

例外例外
例外種類条件

InvalidOperationException

現在の型は型パラメータ表しません。つまり、IsGenericParameter は false返します

解説解説

GenericParameterPosition プロパティは、型が最初に定義されジェネリック型定義またはジェネリック メソッド定義のパラメータ リスト内での型パラメータ位置返します。DeclaringType プロパティおよび DeclaringMethod プロパティジェネリック型定義またはジェネリック メソッド定義を識別します。

GenericParameterPosition プロパティの値の正しコンテキスト提供するには、型パラメータ属すジェネリック型またはジェネリック メソッド識別する必要があります。たとえば、次のコード例ジェネリック メソッド GetSomething戻り値について考えてましょう

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
public class B<T, U> { }
public class A<V>
{
    public B<V, X> GetSomething<X>()
    {
        return new B<V, X>();
    }
}
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 Base<V, X>();
    }
};

GetSomething によって返される型は、A および GetSomething そのもの提供される型引数によって決まりますGetSomethingMethodInfo取得してから、戻り値の型を取得できます戻り値の型の型パラメータ調べると、GenericParameterPosition両方に 0 を返しますVクラス A型パラメータ リスト内の最初型パラメータなので、V位置は 0 です。XGetSomething型パラメータ リスト内の最初型パラメータなので、X位置は 0 です。

メモメモ

GenericParameterPosition プロパティ呼び出したときに、現在の Type型パラメータ表してない場合例外発生しますオープン構築型の型引数調べ場合は、IsGenericParameter プロパティ使用して、どちらが型パラメータでどちらが型であるかを識別できますIsGenericParameter プロパティによって型パラメータtrue返されたら、GenericParameterPosition メソッド位置取得しDeclaringMethod プロパティおよび DeclaringType プロパティ使ってジェネリック メソッド定義またはジェネリック型定義決定します

使用例使用例

2 つの型パラメータ指定したジェネリック クラス、および最初クラスから派生する 2 番目のジェネリック クラス定義する例を次に示します派生クラス基本クラスには 2 つの型引数あります最初型引数は Int32 で、2 番目の型引数派生型型パラメータです。この例では、GenericParameterPosition プロパティによって報告され位置を含む、ジェネリック クラス情報示されています。

Imports System
Imports System.Reflection
Imports System.Collections.Generic
Imports Microsoft.VisualBasic

' 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 'Main
    
    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 'DisplayGenericTypeInfo
End Class 'Test

' 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)
' 
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)
 */
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)
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からType.GenericParameterPosition プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からType.GenericParameterPosition プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からType.GenericParameterPosition プロパティ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Type.GenericParameterPosition プロパティ」の関連用語

Type.GenericParameterPosition プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Type.GenericParameterPosition プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS