ExpressionDelegates.Generation 1.0.0

ExpressionDelegates is a tool which generates delegates to properties, fields, methods and constructors used in linq expression trees.

The purpose of this library is to compile the most common and simplest operations in expression trees during the build  to avoid their slow compilation at runtime or invoсation overhead after interpretation.

If your project doesn't have linq expressions to generate delegates from and you just want to use the generated delegates from other assemblies, please install `ExpressionDelegates.Base` NuGet package to avoid any code generation attempts.

Install-Package ExpressionDelegates.Generation -Version 1.0.0
dotnet add package ExpressionDelegates.Generation --version 1.0.0
<PackageReference Include="ExpressionDelegates.Generation" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExpressionDelegates.Generation --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ExpressionDelegates.Generation, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ExpressionDelegates.Generation as a Cake Addin
#addin nuget:?package=ExpressionDelegates.Generation&version=1.0.0

// Install ExpressionDelegates.Generation as a Cake Tool
#tool nuget:?package=ExpressionDelegates.Generation&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Usage

API is very simple:

ExpressionDelegates.Accessors.Find(string signature).Getter(object targetObject)
ExpressionDelegates.Accessors.Find(MemberInfo member).Setter(object targetObject, object value)

ExpressionDelegates.Methods.Find(string signature).Invoke(object targetObject, params object[] args)
ExpressionDelegates.Methods.Find(MethodInfo method).Invoke(object targetObject, params object[] args)

ExpressionDelegates.Constructors.Find(string signature ).Invoke(params object[] args)
ExpressionDelegates.Constructors.Find(Constructors constructor).Invoke(params object[] args)

Property/Field getter and setter delegates

Expression<Func<string, int>> expression = s => s.Length;
MemberInfo accessorInfo = ((MemberExpression)expression.Body).Member;

Accessor lengthAccessor = ExpressionDelegates.Accessors.Find(accessorInfo);
// or
lengthAccessor = ExpressionDelegates.Accessors.Find("System.String.Length");

var value = lengthAccessor.Get("17 letters string");
// value == 17

If property/field is read-only, Accessor.Set will be null.

If property is write-only, Accessor.Get will be null.

Method delegates

Expression<Func<string, char, bool>> expression = (s, c) => s.Contains(c);
MethodInfo methodInfo = ((MethodCallExpression)expression.Body).Method;

Method containsMethod = ExpressionDelegates.Methods.Find(methodInfo);
// or
containsMethod = ExpressionDelegates.Methods.Find("System.String.Contains(System.Char)");

var value = containsMethod.Invoke("Hello", 'e');
// value == true

Constructor delegates

Expression<Func<char, int, string>> expression = (c, i) => new string(c, i);
ConstructorInfo ctorInfo = ((NewExpression)expression.Body).Constructor;

Constructor stringCtor = ExpressionDelegates.Constructors.Find(ctorInfo);
// or
stringCtor = ExpressionDelegates.Constructors.Find("System.String.String(System.Char, System.Int32)");

var value = stringCtor.Invoke('c', 5);
// value == "ccccc"

Possibilities and Limitations

Parameters for the delegates, their count and type checking are up to you. InvalidCastException is thrown in case of using wrong parameters for a delegate signature. IndexOutOfRangeException is thrown for an unexpected parameter count.

  • No delegates for private or protected classes and members, only for internal and public

  • No delegates for methods or constructors with ref parameters

  • No delegates for anonymous types (not sure if needed)

  • Static members are supported

  • Generics are supported

  • Nested classes are supported

  • Dynamic properties/fields are supported

Usage

API is very simple:

ExpressionDelegates.Accessors.Find(string signature).Getter(object targetObject)
ExpressionDelegates.Accessors.Find(MemberInfo member).Setter(object targetObject, object value)

ExpressionDelegates.Methods.Find(string signature).Invoke(object targetObject, params object[] args)
ExpressionDelegates.Methods.Find(MethodInfo method).Invoke(object targetObject, params object[] args)

ExpressionDelegates.Constructors.Find(string signature ).Invoke(params object[] args)
ExpressionDelegates.Constructors.Find(Constructors constructor).Invoke(params object[] args)

Property/Field getter and setter delegates

Expression<Func<string, int>> expression = s => s.Length;
MemberInfo accessorInfo = ((MemberExpression)expression.Body).Member;

Accessor lengthAccessor = ExpressionDelegates.Accessors.Find(accessorInfo);
// or
lengthAccessor = ExpressionDelegates.Accessors.Find("System.String.Length");

var value = lengthAccessor.Get("17 letters string");
// value == 17

If property/field is read-only, Accessor.Set will be null.

If property is write-only, Accessor.Get will be null.

Method delegates

Expression<Func<string, char, bool>> expression = (s, c) => s.Contains(c);
MethodInfo methodInfo = ((MethodCallExpression)expression.Body).Method;

Method containsMethod = ExpressionDelegates.Methods.Find(methodInfo);
// or
containsMethod = ExpressionDelegates.Methods.Find("System.String.Contains(System.Char)");

var value = containsMethod.Invoke("Hello", 'e');
// value == true

Constructor delegates

Expression<Func<char, int, string>> expression = (c, i) => new string(c, i);
ConstructorInfo ctorInfo = ((NewExpression)expression.Body).Constructor;

Constructor stringCtor = ExpressionDelegates.Constructors.Find(ctorInfo);
// or
stringCtor = ExpressionDelegates.Constructors.Find("System.String.String(System.Char, System.Int32)");

var value = stringCtor.Invoke('c', 5);
// value == "ccccc"

Possibilities and Limitations

Parameters for the delegates, their count and type checking are up to you. InvalidCastException is thrown in case of using wrong parameters for a delegate signature. IndexOutOfRangeException is thrown for an unexpected parameter count.

  • No delegates for private or protected classes and members, only for internal and public

  • No delegates for methods or constructors with ref parameters

  • No delegates for anonymous types (not sure if needed)

  • Static members are supported

  • Generics are supported

  • Nested classes are supported

  • Dynamic properties/fields are supported

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 131 11/12/2020