2

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

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在调用方法需要它们时生成。


现在我想填充对象的PropertyValue属性PropertyContext

为了获得属性的值,我调用了像这样的其他组件

_propertyValueAccessor.GetValue(_target, property)  

我的问题是,我如何以*

  • 该值仅读取一次
  • 但前提是创建了 PropertyContext
4

1 回答 1

3

怎么样:

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,
            };
于 2011-03-04T14:09:44.400 回答