系列文章: C# 表达式树及其应用 (Expression 类)
本节继续讲解Expression相关的API, 力争把相关api 都使用简短demo说明一下
- Assign 创建一个表示赋值运算的 BinaryExpression。
//Assign 创建一个表示赋值运算的 BinaryExpression。
void AssignSample()
{
ParameterExpression variableAssignExpr = Expression.Variable(typeof(String), "sampleVar");
Expression assignSimpleExpr = Expression.Assign(
variableAssignExpr,
Expression.Constant("Hello World!")
);
Expression blockAssignExpr = Expression.Block(
new ParameterExpression[] { variableAssignExpr },
assignSimpleExpr
);
Console.WriteLine(assignSimpleExpr.ToString());//(sampleVar = "Hello World!")
Console.WriteLine(Expression.Lambda<Func<String>>(blockAssignExpr).Compile()());//Hello World!
}
AssignSample();
- Bind 创建一个表示成员初始化的 MemberAssignment。
//Bind 创建一个表示成员初始化的 MemberAssignment。
public class TestBindMember
{
public int Num { get; set; }
}
void BindSample()
{
var variableBindExpr = Expression.Parameter(typeof(int), "number");
var newTestBindMember = Expression.New(typeof(TestBindMember));
//MemberInfo testBindMemberInfo = testBindMember.GetType().GetMember("Num").FirstOrDefault()!;
var bindsExpr = new[] {
Expression.Bind(typeof(TestBindMember).GetProperty("Num")!,variableBindExpr)
};
var bindsBody = Expression.MemberInit(newTestBindMember, bindsExpr);
var bindsFunc = Expression.Lambda<Func<int, TestBindMember>>(bindsBody, new[] { variableBindExpr }).Compile();
bindsBody.ToString().Dump();//new TestBindMember() {Num = number}
var testBindMember = bindsFunc(1111);
Console.WriteLine(testBindMember.GetType() == typeof(TestBindMember));//True
testBindMember.Num.Dump();//111
}
BindSample();
- Block 创建一个 BlockExpression。当执行块表达式时,它将返回块中最后一个表达式的值
public class Person
{
public string Name { get; set; } = null!;
public string GetName() => this.Name;
public string GetOrSetName(string name)
{
if (this.Name == null) this.Name = name;
return this.Name;
}
}
//Block 创建一个 BlockExpression。当执行块表达式时,它将返回块中最后一个表达式的值
void BlockSample()
{
ParameterExpression varExpr = Expression.Variable(typeof(int), "sampleVar");
BlockExpression blockAssignAddExpr = Expression.Block(
new ParameterExpression[] { varExpr },
Expression.Assign(varExpr, Expression.Constant(1)),
Expression.Add(varExpr, Expression.Constant(5))
);
blockAssignAddExpr.Expressions.ForEach(p => p.ToString().Dump()); //(sampleVar = 1) , (sampleVar + 5)
var compileBlockExpr = Expression.Lambda<Func<int>>(blockAssignAddExpr).Compile()().Dump();//6
"创建块表达式".Dump();
Person personBlock = new Person();
//personBlock.Name = "wwmin";
Type[] types = new Type[1];
types[0] = typeof(string);
BlockExpression blockExpr = Expression.Block(
Expression.Call(
null,
typeof(Console).GetMethod("Write", new Type[] { typeof(String) })!,
Expression.Constant("Hello World!")
)
);
blockExpr.Expressions.ForEach(p => p.ToString().Dump());//Write("Hello World!")
Expression.Lambda<Action>(blockExpr).Compile()();//Hello World!
}
BlockSample();
本文作者:wwmin
微信公众号: DotNet技术说
本文链接:https://www.jianshu.com/p/f661e2102019
关于博主:评论和私信会在第一时间回复。或者[直接私信]我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,关注点赞, 您的鼓励是博主的最大动力!
网友评论