Fasterflect

repository·master·Indexed 43 days ago

https://github.com/buunguyen/fasterflect

A high-performance .NET reflection library that uses lightweight dynamic code generation to speed up member access and method invocation. It provides intuitive extension methods for Type and Object, support for ref/out parameters, and cached delegates for maximum performance. Key features include querying assemblies, accessing fields and properties, and high-level services for deep object cloning, materialization, and mapping.

Tokens
1.2K
Snippets
3
Records
4
Agent score
5%

What's inside Fasterflect

  1. Overview of Fasterflect functionality

    master

    Fasterflect is a high-performance reflection library for .NET that addresses the usability and performance limitations of native reflection. It uses lightweight dynamic code generation to achieve speeds 5 to hundreds of times faster than native .NET reflection.

    It provides three main functional areas:

    1. Querying: Search for types in assemblies, find methods by partial names, or locate constructors.
    2. Accessing: Perform reflective invocations on constructors, indexers, fields, properties, and methods for both reference types (including arrays) and structs. It supports ref/out parameters and automatic parameter type inference.
    3. Services: High-level abstractions for deep object cloning, object materialization (construction without default constructors), object mapping, and XML serialization.
  2. Optimize performance using Cached Delegates

    master

    For maximum performance, instead of using the extension methods directly (which perform lookups each time), you can use the DelegateFor... methods to create cached delegates. This is the fastest way to use Fasterflect as it bypasses repeated metadata lookups.

    Common Cached Delegates

    • type.DelegateForGetStaticFieldValue(name): Returns a StaticMemberGetter.
    • type.DelegateForCreateInstance(paramTypes): Returns a ConstructorInvoker.
    • type.DelegateForSetPropertyValue(name): Returns a MemberSetter.
    • type.DelegateForGetPropertyValue(name): Returns a MemberGetter.
    • type.DelegateForCallMethod(name, paramTypes): Returns a MethodInvoker.
    • type.DelegateForMap(targetType): Returns a delegate for high-speed object mapping.

    Usage Pattern

    1. Create the delegate once using the Type extension.
    2. Store the delegate.
    3. Invoke the delegate repeatedly in loops or high-frequency code paths.
    // 1. Create the cached delegate
    ConstructorInvoker ctor = type.DelegateForCreateInstance(new[] { typeof(int), typeof(string) });
    MethodInvoker walk = type.DelegateForCallMethod("Walk", new[] { typeof(int) });
    
    // 2. Use the delegate repeatedly for high performance
    for (int i = 0; i < 100; i++)
    {
        object person = ctor(i, "Name");
        walk(person, 10);
    }
  3. Access members using extension methods

    master

    Fasterflect provides intuitive extension methods for Type and Object to perform common reflection tasks without the verbosity of the standard .NET API.

    Working with Type

    Use Type extensions to create instances, access static members, or invoke methods:

    • type.CreateInstance(...): Invokes a constructor.
    • type.GetFieldValue(name) / type.SetFieldValue(name, value): Accesses fields.
    • type.GetPropertyValue(name) / type.SetPropertyValue(name, value): Accesses properties.
    • type.CallMethod(name, ...args): Invokes a method.
    • type.MakeArrayType(): Creates a type representing an array of the current type.

    Working with Object

    Use Object extensions to access members of a specific instance:

    • obj.GetFieldValue(name) / obj.SetFieldValue(name, value)
    • obj.GetPropertyValue(name) / obj.SetPropertyValue(name, value)
    • obj.CallMethod(name, ...args)
    • obj.GetIndexer(index): Accesses an indexer.
    • obj.MapProperties(target): Maps properties from the current object to a target object.

    Handling ref/out parameters

    When invoking methods with ref or out parameters, pass the arguments in an object[] and explicitly specify the parameter types using MakeByRefType() to ensure correct invocation.

    // Invoke method receiving ref/out params
    var arguments = new object[] { 1, 2 };
    type.CallMethod("Swap", 
        new[] { typeof(int).MakeByRefType(), typeof(int).MakeByRefType() },
        arguments);
    
    // Access indexer
    char c = (char)obj.GetIndexer(1);
    
    // Map properties from an anonymous type to an object
    new { Id = 4, Name = "Nguyen" }.MapProperties(obj);