Leer en inglés

Compartir a través de


Type.DeclaringMethod Propiedad

Definición

Obtiene un objeto MethodBase que representa el método declarativo si el objeto Type actual representa un parámetro de tipo de un método genérico.

C#
public virtual System.Reflection.MethodBase? DeclaringMethod { get; }
C#
public virtual System.Reflection.MethodBase DeclaringMethod { get; }

Valor de propiedad

Si el objeto Type actual representa un parámetro de tipo de un método genérico, MethodBase que representa el método de declaración; de lo contrario, null.

Ejemplos

En el ejemplo de código siguiente se define una clase que tiene un método genérico, se asigna un argumento de tipo al método e se invoca el método genérico construido resultante. También muestra información sobre la definición de método genérico y el método construido. Al mostrar información sobre los parámetros de tipo de la definición de método genérico, en el DisplayGenericMethodInfo método , el código de ejemplo muestra el valor de la DeclaringMethod propiedad para el parámetro de tipo genérico del método.

C#
using System;
using System.Reflection;

// Define a class with a generic method.
public class Example
{
    public static void Generic<T>(T toDisplay)
    {
        Console.WriteLine("\r\nHere it is: {0}", toDisplay);
    }
}

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Examine a generic method.");

        // Create a Type object representing class Example, and
        // get a MethodInfo representing the generic method.
        //
        Type ex = typeof(Example);
        MethodInfo mi = ex.GetMethod("Generic");

        DisplayGenericMethodInfo(mi);

        // Assign the int type to the type parameter of the Example
        // method.
        //
        MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));

        DisplayGenericMethodInfo(miConstructed);

        // Invoke the method.
        object[] args = {42};
        miConstructed.Invoke(null, args);

        // Invoke the method normally.
        Example.Generic<int>(42);

        // Get the generic type definition from the closed method,
        // and show it's the same as the original definition.
        //
        MethodInfo miDef = miConstructed.GetGenericMethodDefinition();
        Console.WriteLine("\r\nThe definition is the same: {0}",
            miDef == mi);
    }

    private static void DisplayGenericMethodInfo(MethodInfo mi)
    {
        Console.WriteLine("\r\n{0}", mi);

        Console.WriteLine("\tIs this a generic method definition? {0}",
            mi.IsGenericMethodDefinition);

        Console.WriteLine("\tIs it a generic method? {0}",
            mi.IsGenericMethod);

        Console.WriteLine("\tDoes it have unassigned generic parameters? {0}",
            mi.ContainsGenericParameters);

        // If this is a generic method, display its type arguments.
        //
        if (mi.IsGenericMethod)
        {
            Type[] typeArguments = mi.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}  parameter position {1}" +
                        "\n\t\t   declaring method: {2}",
                        tParam,
                        tParam.GenericParameterPosition,
                        tParam.DeclaringMethod);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }
}

/* This example produces the following output:

--- Examine a generic method.

Void Generic[T](T)
        Is this a generic method definition? True
        Is it a generic method? True
        Does it have unassigned generic parameters? True
        List type arguments (1):
                T  parameter position 0
                   declaring method: Void Generic[T](T)

Void Generic[Int32](Int32)
        Is this a generic method definition? False
        Is it a generic method? True
        Does it have unassigned generic parameters? False
        List type arguments (1):
                System.Int32

Here it is: 42

Here it is: 42

The definition is the same: True

 */

Comentarios

El método declarante es una definición de método genérico. Es decir, si DeclaringMethod no devuelve null, DeclaringMethod.IsGenericMethodDefinition devuelve true.

Las DeclaringType propiedades y DeclaringMethod identifican la definición de tipo genérico o la definición de método genérico en la que se definió originalmente el parámetro de tipo genérico:

El MethodBase que devuelve la DeclaringMethod propiedad es en MethodInfo el caso de un método genérico o en ConstructorInfo el caso de un constructor genérico.

Nota

En .NET Framework versión 2.0, no se admiten constructores genéricos.

Para obtener una lista de las condiciones invariables para los términos usados en la reflexión genérica, vea los comentarios de la propiedad IsGenericType.

Se aplica a

Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Consulte también