给定一个类中的属性,带有属性 - 确定它是否包含给定属性的最快方法是什么?例如:
[IsNotNullable] [IsPK] [IsIdentity] [SequenceNameAttribute("Id")] public Int32 Id { get { return _Id; } set { _Id = value; } }
确定例如它具有“IsIdentity”属性的最快方法是什么?
没有快速的方法来检索属性。但是代码应该是这样的:
var t = typeof(YourClass); var pi = t.GetProperty("Id"); var hasIsIdentity = Attribute.IsDefined(pi, typeof(IsIdentity));
如果您需要检索属性属性,那么
var t = typeof(YourClass); var pi = t.GetProperty("Id"); var attr = (IsIdentity[])pi.GetCustomAttributes(typeof(IsIdentity), false); if (attr.Length > 0) { // Use attr[0], you'll need foreach on attr if MultiUse is true }