pok*_*oke 3 .net c# reflection .net-4.0
我试图找出绑定表达式的源属性类型.我想这样做是因为我想使用UpdateSourceExceptionFilter来提供比通用"无法转换"更有用的错误消息.
在.NET 4.5中,我使用ResolvedSource和ResolvedSourcePropertyName反射来获取源属性类型,如下所示:
PropertyInfo sourceProperty = expr.ResolvedSource.GetType().GetProperty(expr.ResolvedSourcePropertyName);
Type propertyType = sourceProperty.PropertyType;
Run Code Online (Sandbox Code Playgroud)
这很好用.然而,这两个BindingExpression属性都是在.NET 4.5中引入的,而我仍然在4.0上(因为Windows XP而无法真正更新).
那么在.NET 4.0中有一个很好的方法吗?我想过使用反射获取内部SourceItem和SourcePropertyName属性,或者只是私有Worker来获取这些值,但我宁愿避免访问内部/私有属性或字段(我想这也需要我做一些关于信任的事情?有什么影响?).
不太漂亮,但无法访问私有方法:
string[] splits = expr.ParentBinding.Path.Path.Split('.');
Type type = expr.DataItem.GetType();
foreach (string split in splits) {
type = type.GetProperty(split).PropertyType;
}
Run Code Online (Sandbox Code Playgroud)
因此,我们能够解析源属性.
| 归档时间: |
|
| 查看次数: |
2372 次 |
| 最近记录: |