if (attributeType == null)
throw new ArgumentNullException ("attributeType");
- if (IsUserCattrProvider (obj))
- return obj.IsDefined (attributeType, inherit);
+ AttributeUsageAttribute usage = null;
+ do {
+ if (IsUserCattrProvider (obj))
+ return obj.IsDefined (attributeType, inherit);
+
+ if (IsDefinedInternal (obj, attributeType))
+ return true;
+
+ object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
+ if (pseudoAttrs != null) {
+ for (int i = 0; i < pseudoAttrs.Length; ++i)
+ if (attributeType.IsAssignableFrom (pseudoAttrs[i].GetType ()))
+ return true;
+ }
- if (IsDefinedInternal (obj, attributeType))
- return true;
+ if (usage == null) {
+ if (!inherit)
+ return false;
- object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
- if (pseudoAttrs != null) {
- for (int i = 0; i < pseudoAttrs.Length; ++i)
- if (attributeType.IsAssignableFrom (pseudoAttrs [i].GetType ()))
- return true;
- }
+ usage = RetrieveAttributeUsage (attributeType);
+ if (!usage.Inherited)
+ return false;
+ }
-#if ONLY_1_1
- if (inherit) {
- AttributeUsageAttribute usage = RetrieveAttributeUsage (attributeType);
- if (!usage.Inherited)
- inherit = false;
- }
-#endif
-
- // FIXME (bug #82431):
- // on 2.0 profile we should always walk the inheritance
- // chain and base the behavior on the inheritance level:
- //
- // 0 : return true if "attributeType" is assignable from
- // any of the custom attributes
- //
- // > 0: return true if "attributeType" is assignable from
- // any of the custom attributes and AttributeUsageAttribute
- // .Inherited of the assignable attribute is true
-
- ICustomAttributeProvider btype;
- if (inherit && ((btype = GetBase (obj)) != null))
- return IsDefined (btype, attributeType, inherit);
+ obj = GetBase (obj);
+ } while (obj != null);
return false;
}
{
}
}
+
+ class MyDerivedClassNoAttribute : MyClass
+ {
+ }
}
[TestFixture]
Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
+ Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClassNoAttribute), typeof (MyCustomAttribute)), "#11");
}
[Test]
public void IsDefined_PropertyInfo_Override ()
{
PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
-#if NET_2_0
Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
-#else
- Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
-#endif
Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
-#if NET_2_0
Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
-#else
- Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
-#endif
Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");