Ler em inglês

Compartilhar via


Type.DeclaringMethod Propriedade

Definição

Obtém um MethodBase que representa o método declarativo, se o Type atual representa um parâmetro de tipo de um método genérico.

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

Valor da propriedade

MethodBase

Se o Type atual representa um parâmetro de tipo de um método genérico, um MethodBase que representa o método declarativo; caso contrário, null.

Exemplos

O exemplo de código a seguir define uma classe que tem um método genérico, atribui um argumento de tipo ao método e invoca o método genérico construído resultante. Ele também exibe informações sobre a definição de método genérico e o método construído. Ao exibir informações sobre os parâmetros de tipo da definição de método genérico, no método , o código de exemplo mostra o valor da propriedade para o parâmetro de tipo DisplayGenericMethodInfo DeclaringMethod genérico do 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

 */

Comentários

O método declarante é uma definição de método genérico. Ou seja, se DeclaringMethod não retornar null , DeclaringMethod.IsGenericMethodDefinition retornará true .

As propriedades e identificam a definição de tipo genérico ou a definição de método genérico na qual o parâmetro de tipo DeclaringType DeclaringMethod genérico foi originalmente definido:

O retornado pela propriedade é um no caso de um método genérico ou um no MethodBase caso de um construtor DeclaringMethod MethodInfo ConstructorInfo genérico.

Observação

No .NET Framework versão 2.0, não há suporte para construtores genéricos.

Para obter uma lista das condições invariáveis para termos usados na reflexão genérica, consulte os comentários da propriedade IsGenericType.

Aplica-se a

Produto Versões
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

Confira também