Table of Contents

Class MethodDescriptor

Namespace
Cuemon.Reflection
Assembly
Cuemon.Core.dll

Provides information about a method, such as its name, parameters and whether its a property or method.

public sealed class MethodDescriptor
Inheritance
MethodDescriptor

Examples

MethodDescriptor provides a structured representation of a method's metadata including its caller type, method name, parameters, and optional runtime arguments. This example creates a MethodDescriptor from string.IndexOf's MethodInfo, inspects its Caller (System.String), MethodName (IndexOf), and Parameters (value and comparisonType), then appends runtime arguments via AppendRuntimeArguments("Hello", StringComparison.OrdinalIgnoreCase). It also demonstrates the static Create factory and MergeParameters to combine parameter signatures with runtime values. Console output displays the caller full name, method name, signature, parameter list, runtime argument key-value pairs, and merged parameter values.

using System;
using System.Linq;
using System.Reflection;
using Cuemon.Reflection;

namespace MyApp.Reflection
{
    public static class MethodDescriptorExamples
    {
        public static void Demonstrate()
        {
            // Create a MethodDescriptor from a MethodInfo.
            MethodInfo methodInfo = typeof(string).GetMethod("IndexOf",
                new[] { typeof(string), typeof(StringComparison) });
            var descriptor = new MethodDescriptor(methodInfo);

            Console.WriteLine("Caller: {0}", descriptor.Caller.FullName);   // System.String
            Console.WriteLine("Method: {0}", descriptor.MethodName);        // IndexOf
            Console.WriteLine("Signature: {0}", descriptor.ToString(true));
            // Output: System.String.IndexOf(String value, StringComparison comparisonType)

            // List all parameters.
            Console.WriteLine("Parameters:");
            foreach (var param in descriptor.Parameters)
            {
                Console.WriteLine("  {0} {1}", param.ParameterType.Name, param.ParameterName);

            // Append runtime arguments for debugging or logging.
            descriptor.AppendRuntimeArguments("Hello", StringComparison.OrdinalIgnoreCase);
            Console.WriteLine("Runtime arguments:");
            foreach (var kvp in descriptor.RuntimeArguments)
            {
                Console.WriteLine("  {0} = {1}", kvp.Key, kvp.Value);

            // Static factory method.
            var fromFactory = MethodDescriptor.Create(
                typeof(Math).GetMethod("Max", new[] { typeof(int), typeof(int) }));
            Console.WriteLine("Factory: {0}", fromFactory.ToString(false));

            // Merge parameters with runtime values.
            var merged = MethodDescriptor.MergeParameters(
                new[] {
                    new ParameterSignature(typeof(string), "input"),
                    new ParameterSignature(typeof(int), "count")
                },
                "test", 3);
            Console.WriteLine("Merged: input={0}, count={1}", merged["input"], merged["count"]);

}}}}
}

Constructors

MethodDescriptor(MethodBase)

Initializes a new instance of the MethodDescriptor class.

public MethodDescriptor(MethodBase method)

Parameters

method MethodBase

The method to extract a signature for.

Exceptions

ArgumentNullException

method is null.

MethodDescriptor(Type, MethodBase)

Initializes a new instance of the MethodDescriptor class.

public MethodDescriptor(Type caller, MethodBase method)

Parameters

caller Type

The class on which the method resides.

method MethodBase

The method to extract a signature for.

Exceptions

ArgumentNullException

method is null.

Properties

Caller

Gets the Type of the class where the Method is located.

public Type Caller { get; }

Property Value

Type

The Type of the class where the Method is located.

Method

Gets the MethodBase of this instance.

public MethodBase Method { get; }

Property Value

MethodBase

The MethodBase of this instance.

MethodName

Gets the name of the method.

public string MethodName { get; }

Property Value

string

The name of the method.

Parameters

Gets the parameters of the method.

public IEnumerable<ParameterSignature> Parameters { get; }

Property Value

IEnumerable<ParameterSignature>

A sequence of type ParameterSignature containing information that matches the signature of the method.

RuntimeArguments

Gets the runtime arguments, if any, that was associated with this instance.

public IReadOnlyDictionary<string, object> RuntimeArguments { get; }

Property Value

IReadOnlyDictionary<string, object>

The runtime arguments, if any, that was associated with this instance.

Methods

AppendRuntimeArguments(params object[])

Associates the specified arguments to this instance.

public MethodDescriptor AppendRuntimeArguments(params object[] arguments)

Parameters

arguments object[]

The runtime arguments to associate with this instance.

Returns

MethodDescriptor

A reference to this instance after the operation has completed.

Create(MethodBase)

Creates and returns a MethodDescriptor object and automatically determines the type of the signature (be that method or property).

public static MethodDescriptor Create(MethodBase method)

Parameters

method MethodBase

The method to extract a signature for.

Returns

MethodDescriptor

A MethodDescriptor object.

Remarks

Although confusing a property is to be thought of as a method with either one or two methods (Get, Set) contained inside the property declaration.

MergeParameters(MethodDescriptor, params object[])

Merges the method parameter signature with the specified runtimeParameterValues.

public static IDictionary<string, object> MergeParameters(MethodDescriptor method, params object[] runtimeParameterValues)

Parameters

method MethodDescriptor

The method holding the parameter signature to merge with the runtime parameter values.

runtimeParameterValues object[]

The runtime parameter values.

Returns

IDictionary<string, object>

An IDictionary<TKey, TValue> containing the merged result of the method parameter signature and runtimeParameterValues.

MergeParameters(IEnumerable<ParameterSignature>, params object[])

Merges the parameters signature with the specified runtimeParameterValues.

public static IDictionary<string, object> MergeParameters(IEnumerable<ParameterSignature> parameters, params object[] runtimeParameterValues)

Parameters

parameters IEnumerable<ParameterSignature>

The parameter signature to merge with the runtime parameter values.

runtimeParameterValues object[]

The runtime parameter values.

Returns

IDictionary<string, object>

An IDictionary<TKey, TValue> containing the merged result of the parameters signature and runtimeParameterValues.

MergeParameters(params object[])

Merges the Parameters signature of this instance with the specified runtimeParameterValues.

public IDictionary<string, object> MergeParameters(params object[] runtimeParameterValues)

Parameters

runtimeParameterValues object[]

The runtime parameter values.

Returns

IDictionary<string, object>

An IDictionary<TKey, TValue> containing the merged result of the Parameters signature of this instance and runtimeParameterValues.

ToString()

Returns a string that represents the method signature.

public override string ToString()

Returns

string

A string that represents the method signature.

ToString(bool)

Returns a string that represents the method signature.

public string ToString(bool fullName)

Parameters

fullName bool

Specify true to use the fully qualified name of the Caller; otherwise, false for the simple name.

Returns

string

A string that represents the method signature.

Remarks

The returned string has the following format:
Method without parameters: [Caller].MethodName
Method with at least one or more parameter: [Caller].[MethodName]([ParameterType] [ParameterName])

Property: [Caller].[MethodName]
Property with at least one indexer: [Caller].[MethodName][[ParameterType] [ParameterName]]