使用 linq 查询对象的属性

发布于 2024-10-20 20:08:29 字数 956 浏览 2 评论 0原文

我想扫描一个类型的属性和带注释的属性,并返回一个具有以下结构的对象

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-10-27 20:08:29

怎么样:

var query = from property in _target.GetType().GetProperties()
            let attributes = Attribute.GetCustomAttributes(property, true)
            where attributes.Any()
            let val = _propertyValueAccessor.GetValue(_target, property)  
            from attribute in attributes
            select new PropertyContext
            {
                PropertyValue = val,
                Annotation = attribute,
                SourceType = _target,
            };

How about:

var query = from property in _target.GetType().GetProperties()
            let attributes = Attribute.GetCustomAttributes(property, true)
            where attributes.Any()
            let val = _propertyValueAccessor.GetValue(_target, property)  
            from attribute in attributes
            select new PropertyContext
            {
                PropertyValue = val,
                Annotation = attribute,
                SourceType = _target,
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文