[英]What is the advantage of setting `BaseAddress` property of `HttpClient`
[英]what's the advantage of Property Info?
在.net FrameWork 3.5中,我们可以使用下面提到的代码获取属性信息。
using System;
using System.Linq.Expressions;
using System.Reflection;
class Foo
{
public string Bar { get; set; }
}
static class Program
{
static void Main()
{
PropertyInfo prop = PropertyHelper<Foo>.GetProperty(x => x.Bar);
}
}
public static class PropertyHelper<T>
{
public static PropertyInfo GetProperty<TValue>(
Expression<Func<T, TValue>> selector)
{
Expression body = selector;
if (body is LambdaExpression)
{
body = ((LambdaExpression)body).Body;
}
switch (body.NodeType)
{
case ExpressionType.MemberAccess:
return (PropertyInfo)((MemberExpression)body).Member;
default:
throw new InvalidOperationException();
}
}
}
这也可以通过创建类的实例并访问属性成员来完成。 那么,物业信息的优势是什么?
PropertyInfo
用于获取类的属性信息。 不需要创建实例。 优点是消除了键入错误的可能性。
Expressions
是完全不同的概念(内部使用Reflection)。 表达式用于将方法主体表示为树结构。 这允许在运行时灵活地创建/调整方法定义。
Queryable类利用Expressions的这种功能在远程源上构建/执行动态查询。
例如,考虑INotifyPropertyChanged
接口。 用于属性更改通知。
通常的实现将属性名称作为字符串参数。 因此在运行时检测到键入错误。 重构也会破坏代码(尽管智能重构工具会解决此问题)。
void RaisePropertyChanged(PropertyChangedEventArgs args)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public string Name
{
set
{
_name = value;
RaisePropertyChanged("Name"); // Property name is specified as string
}
}
更好的实现(虽然性能不高)将属性名称作为Expression
。
void RaisePropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
var handler = PropertyChanged;
if (handler != null)
{
MemberExpression body = selectorExpression.Body as MemberExpression;
handler(this, new PropertyChangedEventArgs(body.Member.Name));
}
}
public string Name
{
set
{
_name = value;
RaisePropertyChanged( () => this.Name); // Property is specified instead of name that removes typing error
}
}
通过剖析表达式获得PropertyInfo的优点在于,它可以为您提供编译时检查并提供更好的重构支持。
例如,如果将属性的名称从Bar更改为Barr,则代码将不再编译,从而使您可以捕获无效的成员访问错误,而无需实际运行应用程序。
如果您知道需要预先访问的确切属性,则可以使用表达式。
我发现表达式在数据绑定方案中特别有用,在这种情况下,您需要指定要绑定到网格列或列表控件的属性的名称。 在这种情况下使用表达式可以使维护成本降低。
这是一个使用表达式通过自己的PropertyHelper类执行网格列格式化的示例。
跳转到GridForm.FormatGrid()以查看重要的位。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace ExpressionSample
{
public class TestEntity
{
public int ID { get; set; }
public string Text { get; set; }
public decimal Money { get; set; }
}
public partial class GridForm : Form
{
public GridForm()
{
this.InitializeComponent();
}
private void GridForm_Load(object sender, EventArgs e)
{
this.FillGrid();
this.FormatGrid();
}
private void FillGrid()
{
this.DataGridView.DataSource = TestDataProducer.GetTestData();
}
private void FormatGrid()
{
var redCellStyle = new DataGridViewCellStyle() { ForeColor = Color.Red };
var moneyCellStyle = new DataGridViewCellStyle() { Format = "$###,###,##0.00" };
this.GridColumn(e => e.ID).Visible = false;
this.GridColumn(e => e.Text).DefaultCellStyle = redCellStyle;
this.GridColumn(e => e.Money).DefaultCellStyle = moneyCellStyle;
}
private DataGridViewColumn GridColumn<TProperty>(Expression<Func<TestEntity, TProperty>> expr)
{
var propInfo = PropertyHelper<TestEntity>.GetProperty(expr);
var column = this.DataGridView.Columns[propInfo.Name];
return column;
}
}
public static class PropertyHelper<T>
{
public static PropertyInfo GetProperty<TValue>(
Expression<Func<T, TValue>> selector)
{
Expression body = selector;
if (body is LambdaExpression)
{
body = ((LambdaExpression)body).Body;
}
switch (body.NodeType)
{
case ExpressionType.MemberAccess:
return (PropertyInfo)((MemberExpression)body).Member;
default:
throw new InvalidOperationException();
}
}
}
public static class TestDataProducer
{
public static IList<TestEntity> GetTestData()
{
var entities = new List<TestEntity>();
for (var i = 1; i <= 10; i++)
{
var testEntity = new TestEntity {
ID = i,
Text = "Entity " + i.ToString(),
Money = i * 100m
};
entities.Add(testEntity);
}
return entities;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.