Giter Site home page Giter Site logo

antiufo / shaman.reflection Goto Github PK

View Code? Open in Web Editor NEW
21.0 4.0 4.0 17 KB

Creates fast, strongly typed delegates for accessing methods, fields and properties. The right overload is automatically chosen based on the type arguments of Func<> or Action<>.

License: MIT License

C# 100.00%

shaman.reflection's Introduction

Shaman.Reflection

Fast/type-safe reflection for C#.

Fast string-based reflection

// Fast reflection (compiled to IL on first use, no argument boxing/array)
var result = something.InvokeFunction("MethodName", "hello world", 42);

// Static method or static constructor invocation
var instance = someType.InvokeFunction(".ctor", "hello world");
var staticProperty = someType.InvokeFunction("get_InternalProperty");

Fast static-typed reflection

This feature makes it possible to easily wrap non-public types and methods from other assemblies.

Use Func<TThis, TArgs..., TResult> for instance methods/properties/fields, and Func<TArgs..., TResult> for static ones (and Action for setters/void methods).

If an argument or return type is non-public, just use object in its Func definition.

class ExampleWrapper
{
    // Use different _OverloadXx fields to allow multiple overloads of the same method 
    public static Func<Example, int, int, int> SomeMethod_Overload1;
    public static Func<Example, double, double, double> SomeMethod_Overload2;

    // Properties
    public static Action<Example, string> set_Name;
    public static Func<Example, string> get_Name;

    static object _dummy = ReflectionHelper.InitializeWrapper(typeof(ExampleWrapper), typeof(Example));
}

Additionally, you can directly use ReflectionHelper.GetWrapper<> methods for creating wrappers and storing them where you prefer.

MakeGenericTypeFast, MakeGenericMethodFast

Every time you call the ordinary MakeGenericXx methods, multiple objects are internally allocated (including the params[] Type array).

After the initial instantiation of a generic type or method, subsequent calls are performed allocation-free.

using Shaman.Runtime;

// Fast generic type instantiation (no intermediate allocations)
Type intType = typeof(int);
Type listOfIntType = typeof(List<>).MakeGenericTypeFast(intType);

shaman.reflection's People

Contributors

antiufo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.