Table of Contents

Class MethodDescriptorExtensions

Namespace
Cuemon.Extensions
Assembly
Cuemon.Extensions.Core.dll

Extension methods for the MethodDescriptor class.

public static class MethodDescriptorExtensions
Inheritance
MethodDescriptorExtensions

Examples

MethodDescriptorExtensions provides extension methods for MethodDescriptor to inspect parameter information and method metadata. This example retrieves MethodInfo for Console.WriteLine(string) (which has a parameter) and Guid.NewGuid() (which has none), creates MethodDescriptor instances via MethodDescriptor.Create, then calls HasParameters() to check each for parameters. Key steps include resolving the method info via reflection and calling HasParameters on each descriptor. Console output confirms True for WriteLine, False for NewGuid, and displays the method name and caller name.

using System;
using System.Reflection;
using Cuemon.Extensions;
using Cuemon.Reflection;

namespace MyApp.Reflection;

public static class MethodDescriptorExtensionsExample
{
    public static void Demonstrate()
    {
        MethodInfo writeLine = typeof(Console).GetMethod(nameof(Console.WriteLine), new[] { typeof(string) });
        MethodInfo newGuid = typeof(Guid).GetMethod(nameof(Guid.NewGuid), BindingFlags.Public | BindingFlags.Static);

        MethodDescriptor withParameters = MethodDescriptor.Create(writeLine);
        MethodDescriptor withoutParameters = MethodDescriptor.Create(newGuid);

        Console.WriteLine(withParameters.HasParameters());
        Console.WriteLine(withoutParameters.HasParameters());
        Console.WriteLine(withParameters.Method.Name);
        Console.WriteLine(withParameters.Caller?.Name);
    }
}

Methods

HasParameters(MethodDescriptor)

Determines whether the underlying method has parameters.

public static bool HasParameters(this MethodDescriptor descriptor)

Parameters

descriptor MethodDescriptor

The MethodDescriptor to extend.

Returns

bool

true if the specified descriptor has parameters; otherwise, false.