Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Created March 27, 2018 09:04
Show Gist options
  • Save ichiroku11/801824e13ec16ab437fdc81cc07836e3 to your computer and use it in GitHub Desktop.
Save ichiroku11/801824e13ec16ab437fdc81cc07836e3 to your computer and use it in GitHub Desktop.
Expressionを組み立ててFuncを作るサンプル
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using Xunit;
namespace Test {
// 参考
// https://www.slideshare.net/Fujiwo/20141026-c-c-expression-tree
public class ExpressionTest {
[Fact]
public void Expression_足し算を構築する() {
// Expression<Func<int, int, int>> lambda = (x, y) => x + y;
// var func = lambda.Compile();
Func<int, int, int> createFunc() {
var x = Expression.Parameter(typeof(int));
var y = Expression.Parameter(typeof(int));
var add = Expression.Add(x, y);
var lambda = Expression.Lambda<Func<int, int, int>>(add, x, y);
return lambda.Compile();
}
var func = createFunc();
var result = func(1, 2);
Assert.Equal(3, result);
}
// テスト用
private class Item {
public int Value { get; set; }
public int GetValue(int value) => value + 1;
}
[Fact]
public void Expression_プロパティアクセスを構築する() {
// Expression<Func<Item, int>> expression = item => item.Value;
// var func = lambda.Compile();
Func<Item, int> createFunc() {
var target = Expression.Parameter(typeof(Item));
var property = Expression.Property(target, "Value");
var lambda = Expression.Lambda<Func<Item, int>>(property, target);
return lambda.Compile();
}
var func = createFunc();
var item = new Item { Value = 1 };
var result = func(item);
Assert.Equal(item.Value, result);
}
[Fact]
public void Expression_メソッド呼び出しを構築する() {
// Expression<Func<Item, int, int>> lambda = (item, param) => item.GetValue(param);
// var func = lambda.Compile();
Func<Item, int, int> createFunc() {
var target = Expression.Parameter(typeof(Item));
var param = Expression.Parameter(typeof(int));
var method = Expression.Call(target, typeof(Item).GetMethod("GetValue"), param);
var lambda = Expression.Lambda<Func<Item, int, int>>(method, target, param);
return lambda.Compile();
}
var func = createFunc();
var item = new Item();
var result = func(item, 2);
Assert.Equal(3, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment