Сценарии

Версия 2.10 от Alexandr Fokin на 2025/05/13 13:01

Компиляция дерева в делегатExpression
    .Lambda(expressionValiarble)
    .Compile();
Простое копирование свойств одного класса в другой 
Маппинг на основе выражения доступа к свойству 
Вызов делегата при построении EpressionTreeExpression Trees and Invoking a Delegate
https://stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate
Объявлений локальной переменной в блоке
(без передачи извне)

переменная типа указана из области видимости, но не определена 
https://question-it.com/questions/717473/peremennaja-tipa-ukazana-iz-oblasti-vidimosti-no-ne-opredelena

 
Типизированый вызов конструктораvar constructorInfo = type.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
var constructorParameter1 = Expression.Parameter(typeof(bool));
var constructorExp = Expression.New(constructorInfo, constructorParameter1);

// Func<bool, object> or Func<bool, T>
var constructorDelegate = Expression
    .Lambda<Func<bool, object>>(
        constructorExp,
        constructorParameter1
        )
    .Compile();
Нетипизированый вызов
public delegate object ConstructorDelegate(object[]? Args);

public static ConstructorDelegate GetConstructorWithArgsDelegate(
    ConstructorInfo constructorInfo)
{
ParameterExpression constructorParameters = Expression.Parameter(typeof(object?[]));

Expression[] parameterExpressions;
{
    ParameterInfo[] parametersInfos = constructorInfo.GetParameters();
    parameterExpressions = new Expression[parametersInfos.Length];

   for (int i = 0; i < parametersInfos.Length; i++)
    {
        ConstantExpression ithIndex = Expression.Constant(i);
        BinaryExpression ithParameter = Expression.ArrayIndex(
            constructorParameters,
            ithIndex);
        UnaryExpression unboxedIthParameter = Expression.Convert(
            ithParameter,
            parametersInfos[i].ParameterType
            );
        parameterExpressions[i] = unboxedIthParameter;
    }
}

NewExpression constructorCallExp = Expression.New(
    constructorInfo,
    parameterExpressions);

var convertResultExp = Expression.Convert(
    constructorCallExp,
   typeof(object)
    );

var constructorHandler = Expression
    .Lambda<ConstructorDelegate>(
        convertResultExp,
        constructorParameters
        )
    .Compile();

return constructorHandler;
}
C# Expression tree: object array to Expression.New() parameters
https://stackoverflow.com/questions/67968927/c-sharp-expression-tree-object-array-to-expression-new-parameters
public delegate object? MethodDelegate(object instance, object[]? Args);

public static MethodDelegate GetMethodWithArgsDelegate(
    MethodInfo methodInfo,
    Type instanceType)
{
ParameterExpression instanceParameter = Expression.Parameter(typeof(object));
ParameterExpression methodParameters = Expression.Parameter(typeof(object?[]));

Expression instanceExp;
Expression[] parameterExpressions;
{
    instanceExp = Expression.Convert(instanceParameter, p.instanceType);

    ParameterInfo[] parametersInfos = p.methodInfo.GetParameters();
    parameterExpressions = new Expression[parametersInfos.Length];

   for (int i = 0; i < parametersInfos.Length; i++)
    {
        ConstantExpression ithIndex = Expression.Constant(i);
        BinaryExpression ithParameter = Expression.ArrayIndex(
            methodParameters,
            ithIndex);
        UnaryExpression unboxedIthParameter = Expression.Convert(
            ithParameter,
            parametersInfos[i].ParameterType
            );
        parameterExpressions[i] = unboxedIthParameter;
    }
}

var callExpression = Expression.Call(
    instanceExp,
    p.methodInfo,
    parameterExpressions
    );

var convertResultExp = Expression.Convert(
    callExpression,
   typeof(object)
    );

var callHandler = Expression
    .Lambda<MethodDelegate>(
        convertResultExp,
        instanceParameter,
        methodParameters
        )
    .Compile();

return callHandler;
}
 

 

Generic enum to int