有对象的列表:C#获取来自列表特定属性的属性对象
List<ConfigurationObjectBase> ObjectRegistry;
具有以下属性和上面的一些对象的与属性饰:
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public sealed class PropertyCanHaveReference : Attribute
{
public PropertyCanHaveReference(Type valueType)
{
this.ValueType = valueType;
}
public Type ValueType { get; set; }
}
现在,我想找到其属性用该属性装饰的所有对象。
尝试下面的代码,好像我做错了:
List<ConfigurationObjectBase> tmplist = ObjectRegistry.Where(o => (o.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(PropertyCanHaveReference), true).Length > 0)));
感谢您的时间。
不应该第二个'Where'是'Any'? –
乍一看你的代码看起来是正确的(尽管你可能想要坚持约定并调用属性类“PropertyCanHaveReferenceAttribute”)。实际上会发生什么“错误”?你会得到哪些错误信息或没有结果?请提供无法运行的示例对象或[最小,完整且可验证的示例](http://stackoverflow.com/help/mcve) –