C# 反射和属性:Bug?我无法解决这个问题
这是在立即控制台中:
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true)
{BridgeStack.DataContracts.RequiredParameterAttribute[0]}
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true).Cast<RequiredParameterAttribute>()
{BridgeStack.DataContracts.RequiredParameterAttribute[0]} [BridgeStack.DataContracts.RequiredParameterAttribute[]]:{BridgeStack.DataContracts.RequiredParameterAttribute[0]}
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true).Cast<RequiredParameterAttribute>().Any()
假
我在应用程序中得到相同的结果。
prop
是 Site
中:
public class AnswerCollectionQuery : IPagedQuery, ISiteQuery, ISortableQuery, IOrderableQuery, IFilteredQuery
{
public int? Page { get; set; }
public int? PageSize { get; set; }
public string Site { get; set; }
[AllowedSortValues(QuerySortEnum.Activity, QuerySortEnum.Creation, QuerySortEnum.Votes)]
public QuerySortEnum? Sort { get; set; }
public object Min { get; set; }
public object Max { get; set; }
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
public QueryOrderEnum? Order { get; set; }
public string Filter { get; set; }
}
Site
依次来自 ISiteQuery
public interface ISiteQuery : IQuery
{
[RequiredParameter]
string Site { get; set; }
}
尴尬的地方是控制台显示属性,允许我投射它,但我根本无法检索它,枚举的长度为零,这就是 .Any()
失败的原因,.FirstOrDefault()< /代码>返回
null
、.First()
抛出等。
对这种类型的行为有什么解释吗?
PD:如果我在具体类中用 [RequiredAttribute]
装饰 Site
,这会起作用。但我想把这部分做成界面。
为清楚起见进行更新:
prop
完全来自这里:
public static IEnumerable<PropertyInfo> GetAllProperiesOfObject(object o)
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance;
PropertyInfo[] list = o.GetType().GetProperties(flags);
return list;
}
foreach(Utility.GetAllProperiesOfObject(entity)中的PropertyInfo prop)
这是当< code>prop 变为 Site
This is in the Immediate console:
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true)
{BridgeStack.DataContracts.RequiredParameterAttribute[0]}
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true).Cast<RequiredParameterAttribute>()
{BridgeStack.DataContracts.RequiredParameterAttribute[0]}
[BridgeStack.DataContracts.RequiredParameterAttribute[]]: {BridgeStack.DataContracts.RequiredParameterAttribute[0]}
prop.GetCustomAttributes(typeof(RequiredParameterAttribute),true).Cast<RequiredParameterAttribute>().Any()
false
I get the same results in the application.
prop
is Site
in:
public class AnswerCollectionQuery : IPagedQuery, ISiteQuery, ISortableQuery, IOrderableQuery, IFilteredQuery
{
public int? Page { get; set; }
public int? PageSize { get; set; }
public string Site { get; set; }
[AllowedSortValues(QuerySortEnum.Activity, QuerySortEnum.Creation, QuerySortEnum.Votes)]
public QuerySortEnum? Sort { get; set; }
public object Min { get; set; }
public object Max { get; set; }
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
public QueryOrderEnum? Order { get; set; }
public string Filter { get; set; }
}
Site
in turn comes from ISiteQuery
public interface ISiteQuery : IQuery
{
[RequiredParameter]
string Site { get; set; }
}
The awkward part is that the console shows the attribute, allows me to cast it, but I can't retrieve it at all, I get zero as the enumeration's length, which is why .Any()
fails, too, .FirstOrDefault()
returns null
, .First()
throws, etc.
Any explanation for this type of behavior?
PD: this works though if I decorate Site
with [RequiredAttribute]
in the concrete class. But I wanted to make this part of the interface.
Update for clarity:
prop
comes exactly from here:
public static IEnumerable<PropertyInfo> GetAllProperiesOfObject(object o)
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance;
PropertyInfo[] list = o.GetType().GetProperties(flags);
return list;
}
foreach (PropertyInfo prop in Utility.GetAllProperiesOfObject(entity))
This is the case for when prop
becomes Site
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
零是因为它返回一个零长度类型数组,这意味着:它没有该属性。您还可以使用 Attribute.IsDefined 查看这一点(它将返回 false)。
当使用隐式接口实现时,类的公共属性不会自动从它所满足的接口获取属性。要查看接口上的属性,您需要使用
接口上的 Site 属性与类上的 Site 属性无关。如果类上的属性必须具有属性:显式添加属性。
The zero is because it is returning you a zero-length typed array, meaning: it doesn't have the attribute. You can also see this with Attribute.IsDefined (which will return false).
When using implicit interface implementation, the public property on the class does not automatically gain attributes from the interface that it satisfies. To see the attributes on the interface you would need to use
The Site property on the interface is unrelated to the Site property on the class. If the property on the class must have attributes: add the attributes explicitly.