共用方式為


Type.ContainsGenericParameters Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value indicating whether the current Type object has type parameters that have not been replaced by specific types.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property ContainsGenericParameters As Boolean
public virtual bool ContainsGenericParameters { get; }

Property Value

Type: System.Boolean
true if the Type object is itself a generic type parameter or has type parameters for which specific types have not been supplied; otherwise, false.

Remarks

In order to create an instance of a type, there must be no generic type definitions or open constructed types in the type arguments of the type itself, in any enclosing generic types, or in any elements of the type. Another way of saying this is that when examined recursively, the type must contain no generic type parameters.

Since types can be arbitrarily complex, making this determination is difficult. For convenience and to reduce the chance of error, the ContainsGenericParameters property provides a standard way to distinguish between closed constructed types, which can be instantiated, and open constructed types, which cannot. If the ContainsGenericParameters property returns true, the type cannot be instantiated.

The ContainsGenericParameters property searches recursively for type parameters. For example, it returns true for an array whose elements are type A<T> (A(Of T) in Visual Basic), even though the array is not itself generic. Contrast this with the behavior of the IsGenericType property, which returns false for arrays.

For a set of example classes and a table showing the values of the ContainsGenericParameters property, see IsGenericType.

Examples

The following example defines a generic class with two type parameters and then defines a second generic class that derives from the first class. The derived class's base class has two type arguments: the first is Int32 and the second is a type parameter of the derived type. The example displays information about these generic classes, including the positions reported by the GenericParameterPosition property.

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 Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= vbCrLf _
          & "--- Display a generic type and the open constructed" & vbCrLf
      outputBlock.Text &= "    type from which it is derived." & vbCrLf

      ' 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(outputBlock, derivedType)

      ' Display its open constructed base type.
      DisplayGenericTypeInfo(outputBlock, derivedType.BaseType)

   End Sub 'Main

   Private Shared Sub DisplayGenericTypeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal t As Type)
      outputBlock.Text &= String.Format(vbCrLf & "{0}", t) & vbCrLf

      outputBlock.Text &= vbTab & "Is this a generic type definition? " _
          & t.IsGenericTypeDefinition & vbCrLf

      outputBlock.Text &= vbTab & "Is it a generic type? " _
          & t.IsGenericType & vbCrLf

      outputBlock.Text &= vbTab _
          & "Does it have unassigned generic parameters? " _
          & t.ContainsGenericParameters & vbCrLf

      If t.IsGenericType Then
         ' If this is a generic type, display the type arguments.
         '
         Dim typeArguments As Type() = t.GetGenericArguments()

         outputBlock.Text &= vbTab & "List type arguments (" _
             & typeArguments.Length & "):" & vbCrLf

         For Each tParam As Type In typeArguments
            ' IsGenericParameter is true only for generic type
            ' parameters.
            '
            If tParam.IsGenericParameter Then
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() & _
                   "  (unassigned - parameter position " _
                   & tParam.GenericParameterPosition & ")"
            Else
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() & vbCrLf
            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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text +=
          "\r\n--- Display a generic type and the open constructed" + "\n";
      outputBlock.Text += "    type from which it is derived." + "\n";

      // 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(outputBlock, derivedType);

      // Display its open constructed base type.
      DisplayGenericTypeInfo(outputBlock, derivedType.BaseType);
   }

   private static void DisplayGenericTypeInfo(System.Windows.Controls.TextBlock outputBlock, Type t)
   {
      outputBlock.Text += String.Format("\r\n{0}", t) + "\n";

      outputBlock.Text += String.Format("\tIs this a generic type definition? {0}",
          t.IsGenericTypeDefinition) + "\n";

      outputBlock.Text += String.Format("\tIs it a generic type? {0}",
          t.IsGenericType) + "\n";

      outputBlock.Text += String.Format("\tDoes it have unassigned generic parameters? {0}",
          t.ContainsGenericParameters) + "\n";

      if (t.IsGenericType)
      {
         // If this is a generic type, display the type arguments.
         //
         Type[] typeArguments = t.GetGenericArguments();

         outputBlock.Text += String.Format("\tList type arguments ({0}):",
             typeArguments.Length) + "\n";

         foreach (Type tParam in typeArguments)
         {
            // IsGenericParameter is true only for generic type
            // parameters.
            //
            if (tParam.IsGenericParameter)
            {
               outputBlock.Text += String.Format(
                   "\t\t{0}  (unassigned - parameter position {1})",
                   tParam,
                   tParam.GenericParameterPosition) + "\n";
            }
            else
            {
               outputBlock.Text += String.Format("\t\t{0}", tParam) + "\n";
            }
         }
      }
   }
}

/* 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)
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.