Git Product home page Git Product logo

rocklib.reflection.optimized's Introduction

RockLib.Reflection.Optimized

Extension methods to improve reflection performance.

Table of Contents


Supported Targets

This library supports the following targets:

  • .NET 6
  • .NET Core 3.1
  • .NET Framework 4.8

PropertyInfo and FieldInfo extension methods

RockLib.Reflection.Optimized has extension methods for two types: System.Reflection.PropertyInfo and System.Reflection.FieldInfo. These extension methods create functions at runtime that get or set the specified property or field.

CreateGetter / CreateSetter

The following code snippet demonstrates usage of the CreateGetter and CreateSetter extension methods for PropertyInfo (usage for FieldInfo is identical):

using System.Reflection;
using RockLib.Reflection.Optimized;

public class Foo
{
    public int Bar { get; set; }
}

void Main()
{
    PropertyInfo property = typeof(Foo).GetProperty("Bar");
    
    Action<object, object> setBar = property.CreateSetter();
    Func<object, object> getBar = property.CreateGetter();

    Foo foo = new Foo();

    setBar(foo, 123); // Sets the value of the Bar property
    int bar = getBar(foo); // Gets the value of the Bar property
}

Overloads

The CreateGetter and CreateSetter extension methods for PropertyInfo and FieldInfo each have three overloads, allowing the parameters of the resulting delegates to be customized.

Overload Return Type
CreateGetter Func<object, object>
CreateSetter Action<object, object>
CreateGetter<TPropertyType> Func<object, TPropertyType>
CreateSetter<TPropertyType> Action<object, TPropertyType>
CreateGetter<TDeclaringType, TPropertyType> Func<TDeclaringType, TPropertyType>
CreateSetter<TDeclaringType, TPropertyType> Action<TDeclaringType, TPropertyType>

Static properties

If a PropertyInfo or FieldInfo represents a static property or field, then the first parameter of the functions returned by the CreateGetter and CreateSetter methods are ignored. When invokine functions that access static properties or fields, the caller can safely pass null for the first parameter.

CreateStaticGetter / CreateStaticSetter

If it is known that a PropertyInfo or FieldInfo is static, then the CreateStaticGetter and CreateStaticSetter extension methods can be used, as in the following FieldInfo example (usage for PropertyInfo is identical):

using System.Reflection;
using RockLib.Reflection.Optimized;

public static class Foo
{
    public static int Bar;
}

void Main()
{
    FieldInfo field = typeof(Foo).GetField("Bar");
    
    Action<object> setBar = field.CreateStaticSetter();
    Func<object> getBar = field.CreateStaticGetter();

    Foo foo = new Foo();

    setBar(123); // Sets the value of the Foo.Bar property
    int bar = getBar(); // Gets the value of the Foo.Bar property
}

Overloads

Similar to their non-static counterpoints, the CreateStaticGetter and CreateStaticSetter extension methods for PropertyInfo and FieldInfo each have two overloads, allowing the parameters of the resulting delegates to be customized.

Overload Return Type
CreateStaticGetter Func<object>
CreateStaticSetter Action<object>
CreateStaticGetter<TPropertyType> Func<TPropertyType>
CreateStaticSetter<TPropertyType> Action<TPropertyType>

Undecorate extension method

RockLib.Reflection.Optimized also contains an Undecorate extension method. This extension method checks to see if an object implementing an interface is a decorator for that interface, and if it is, unwraps it. An object is considered a decorator if it has an instance field of the same type as the interface it implements. To unwrap a decorator object, the value of its interface instance field is used instead. The following example demonstrates usage of the Undecorate extension method:

void Main()
{
    IFoo foo = new FooDecorator(new AnotherFooDecorator { Foo = new MutableFoo() });
    
    if (foo.Undecorate() is MutableFoo mutableFoo)
        mutableFoo.Bar = 123;
}

public interface IFoo
{
    int Bar { get; }
}

public class MutableFoo : IFoo
{
    public int Bar { get; set; }
}

public class FooDecorator : IFoo
{
    private readonly IFoo _foo;    
    public FooDecorator(IFoo foo) => _foo = foo;
    public int Bar => _foo.Bar;
}

public class AnotherFooDecorator : IFoo
{
    public IFoo Foo { get; set; }
    public int Bar => Foo?.Bar ?? 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.