KGy SOFT

MethodAccessor Class

KGy SOFT Core Libraries Help
Provides an efficient way for invoking methods via dynamically created delegates.
See the Remarks section for details and an example.
Inheritance Hierarchy

SystemObject
  KGySoft.ReflectionMemberAccessor
    KGySoft.ReflectionMethodAccessor

Namespace:  KGySoft.Reflection
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public abstract class MethodAccessor : MemberAccessor

The MethodAccessor type exposes the following members.

Constructors

  NameDescription
Protected methodMethodAccessor
Initializes a new instance of the MethodAccessor class.
Top
Properties

  NameDescription
Public propertyMemberInfo
Gets the reflection member info of the accessed member.
(Inherited from MemberAccessor.)
Top
Methods

  NameDescription
Public methodEquals
Determines whether the specified Object is equal to the current MemberAccessor.
(Inherited from MemberAccessor.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodStatic memberGetAccessor
Gets a MemberAccessor for the specified method.
Public methodGetHashCode
Gets a hash code for the current MemberAccessor instance.
(Inherited from MemberAccessor.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodInvoke
Invokes the method. The return value of Void methods are . For static methods the instance parameter is omitted (can be ).
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a String that represents the current MemberAccessor.
(Inherited from MemberAccessor.)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Top
Remarks

You can obtain a MethodAccessor instance by the static GetAccessor method.

The Invoke method can be used to invoke the method. The first call of this method is slow because the delegate is generated on the first access, but further calls are much faster.

The already obtained accessors are cached so subsequent GetAccessor calls return the already created accessors unless they were dropped out from the cache, which can store about 8000 elements.

Note Note
If you want to invoke a method by name rather then by a MethodInfo, then you can use the InvokeMethod methods in the Reflector class, which have some overloads with a propertyName parameter.
Caution note Caution
The .NET Standard 2.0 version of the Invoke method does not return the ref/out parameters. Furthermore, if an instance method of a value type (struct) mutates the instance, then the changes will not be applied to the instance on which the method is invoked.
If you reference the .NET Standard 2.0 version of the KGySoft.CoreLibraries assembly, then use the Reflector.InvokeMethod overloads to invoke methods with ref/out parameters without losing the returned parameter values and to preserve changes the of the mutated value type instances.
Examples

C#
using System;
using System.Reflection;
using KGySoft.Diagnostics;
using KGySoft.Reflection;

class Example
{
    private class TestClass
    {
        public int TestMethod(int i) => i;
    }

    static void Main(string[] args)
    {
        var instance = new TestClass();
        MethodInfo method = instance.GetType().GetMethod(nameof(TestClass.TestMethod));
        MethodAccessor accessor = MethodAccessor.GetAccessor(method);

        new PerformanceTest { Iterations = 1000000 }
            .AddCase(() => instance.TestMethod(1), "Direct call")
            .AddCase(() => method.Invoke(instance, new object[] { 1 }), "MethodInfo.Invoke")
            .AddCase(() => accessor.Invoke(instance, 1), "MethodAccessor.Invoke")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces a similar output to this one:
// ==[Performance Test Results]================================================
// Iterations: 1,000,000
// Warming up: Yes
// Test cases: 3
// Calling GC.Collect: Yes
// Forced CPU Affinity: 2
// Cases are sorted by time (quickest first)
// --------------------------------------------------
// 1. Direct call: average time: 2.87 ms
// 2. MethodAccessor.Invoke: average time: 26.02 ms (+23.15 ms / 906.97 %)
// 3. MethodInfo.Invoke: average time: 241.47 ms (+238.60 ms / 8,416.44 %)
See Also

Reference