基于 lambda 表达式的反射与普通反射
普通反射和可以使用 lambda 表达式(采用形式 构建您自己的 MVVM):
public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
{
var lambda = (LambdaExpression)property;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else memberExpression = (MemberExpression)lambda.Body;
NotifyOfPropertyChange(memberExpression.Member.Name);
}
基于 lambda 的反射是否仅在内部使用普通反射 API?或者这是有显着不同的东西。它们的性能差异是什么?
What is the difference between normal reflection and the reflection that can be done with lambda expressions such as this (taken form build your own MVVM):
public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
{
var lambda = (LambdaExpression)property;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else memberExpression = (MemberExpression)lambda.Body;
NotifyOfPropertyChange(memberExpression.Member.Name);
}
Is the lambda based reflection just using the normal reflection APIs internally? Or is this something significantly different. What is ther perfomance difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反射的目标是程序集、类和接口结构。它允许访问类定义、方法签名、类型信息等。它不提供对方法代码的访问,无论是抽象语法树 (AST) 还是字节码形式。
Expression<>
类型系列提供对 AST 的直接访问,可用于收集一段代码的结构。这实际上更接近 CodeDOM 工具而不是反射。Reflection targets assembly, class and interface structure. It gives access to class definitions, method signatures, type information, and so on. It doesn't provide access the code of methods, either in Abstract Syntax Tree (AST) or bytecode form.
The
Expression<>
type family provides direct access to an AST, which can be used to glean the structure of a piece of code. This is actually much closer to the CodeDOM facility than to reflection.