Комментарий к Expression tree

Версия 7.7 от Alexandr Fokin на 2022/06/15 12:57

  • Alexandr Fokin
    Alexandr Fokin 2021/04/05 14:05

    Простое копирование свойств одного класса в другой
    //Source
    ClassA a = new ClassA()
    {
        A = 10,
        B = 20
    };
    //Destination
    ClassB b = new ClassB();

    //Source class instance parameter
    var parametrB = Expression.Parameter(b.GetType());

    //Destination class instance parameter
    var parametrA = Expression.Parameter(a.GetType());

    var copyPropertyAEx = Expression.Assign(
        Expression.Property(parametrB, b.GetType().GetProperty("A")),
        Expression.Property(parametrA, a.GetType().GetProperty("A"))
        );
    var copyPropertyBEx = Expression.Assign(
        Expression.Property(parametrB, b.GetType().GetProperty("B")),
        Expression.Property(parametrA, a.GetType().GetProperty("B"))
        );

    var copyPropertyEx = Expression.Block(
        copyPropertyAEx,
        copyPropertyBEx
        );

    var copyPropertyDelegate = Expression
    .Lambda<Func<ClassB, ClassA, int>>(copyPropertyEx, parametrB, parametrA)
    .Compile();

    var result = copyPropertyDelegate.Invoke(b, a);

  • Alexandr Fokin
    Alexandr Fokin 2021/04/05 16:43

    Вызов делегаты при построении EpressionTree
    Expression Trees and Invoking a Delegate
    https://stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate

  • Alexandr Fokin
    Alexandr Fokin 2021/04/05 19:14

    Объявлений локальной переменной в блоке (без передачи извне)
    https://question-it.com/questions/717473/peremennaja-tipa-ukazana-iz-oblasti-vidimosti-no-ne-opredelena