使用 linq 查询对象的属性
我想扫描一个类型的属性和带注释的属性,并返回一个具有以下结构的对象
public class PropertyContext
{
public object PropertyValue { get; set; }
public object SourceType { get; set; }
public Attribute Annotation { get; set; }
}
我有这个查询
var query = from property in _target.GetType().GetProperties()
from attribute in Attribute.GetCustomAttributes(property, true)
select new PropertyContext
{
Annotation = attribute,
SourceType = _target,
};
这是延迟执行的,所以我只在调用方法需要它们时生成 PropertyContext
。
现在我想填充 PropertyContext
对象的 PropertyValue
属性。
为了获取属性的值,我调用了像这样的其他组件
_propertyValueAccessor.GetValue(_target, property)
我的问题是,我如何以这样的方式修改查询 *
- 该值仅
- 在创建 PropertyContext 时读取一次
I want to scan a type for it's properties and the annotated attributes and return an object with the following structure
public class PropertyContext
{
public object PropertyValue { get; set; }
public object SourceType { get; set; }
public Attribute Annotation { get; set; }
}
I have this query
var query = from property in _target.GetType().GetProperties()
from attribute in Attribute.GetCustomAttributes(property, true)
select new PropertyContext
{
Annotation = attribute,
SourceType = _target,
};
This is executed deferred so i only generate the PropertyContext
while the calling method needs them.
Now i want to fill the PropertyValue
property of the PropertyContext
object.
To get the value of the property i have have a call to an other component like this
_propertyValueAccessor.GetValue(_target, property)
My question is, how i can modify the query in a way that
*
- the value is only read once
- but only if a PropertyContext is created
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
How about: