using System;
using System.Reflection;
+#if !MONOTOUCH
using System.Reflection.Emit;
+#endif
using System.Runtime.InteropServices;
using System.Threading;
{
}
}
+
+ class MyDerivedClassNoAttribute : MyClass
+ {
+ }
+
+ internal class AttributeWithTypeId : Attribute
+ {
+ public override object TypeId {
+ get { return this; }
+ }
+ }
}
[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");
Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
}
+ [Test]
+ public void IsDefinedForPseudoAttribute ()
+ {
+ Assert.IsTrue (Attribute.IsDefined (typeof (object), typeof(SerializableAttribute), true), "#1");
+ Assert.IsFalse (Attribute.IsDefined (typeof (AttributeTest), typeof(SerializableAttribute), true), "#2");
+ }
+
+#if !MONOTOUCH
[Test]
public void GetCustomAttributeOnNewSreTypes ()
{
Assert.Fail ("#1");
} catch (NotSupportedException) {}
}
-
+#endif
[Test] //Regression test for #499569
public void GetCattrOnPropertyAndInheritance ()
{
MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
}
+
+ [Test]
+ public void GetHashCodeWithOverriddenTypeId ()
+ {
+ //check for not throwing stack overflow exception
+ AttributeWithTypeId a = new AttributeWithTypeId ();
+ a.GetHashCode ();
+ }
+
+ class ArrayAttribute : Attribute
+ {
+#pragma warning disable 414
+ int[] array;
+#pragma warning restore
+
+ public ArrayAttribute (int[] array)
+ {
+ this.array = array;
+ }
+ }
+
+ [Test]
+ public void ArrayFieldsEquality ()
+ {
+ Assert.IsTrue (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 2 })));
+ Assert.IsFalse (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 1 })));
+ }
}
namespace ParamNamespace {
{
}
}
+
+ class Multiple {
+ public void Bar ([Foo] [Bar] string multiple, [Bar] string bar)
+ {
+ }
+ }
}
[TestFixture]
Assert.AreEqual (1, attributes.Length);
Assert.AreEqual ("Derived.baz", attributes [0].Data);
}
+
+ [Test]
+ public void MultipleParameterAttributes ()
+ {
+ var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "multiple");
+ var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
+ Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ());
+ var bar = parameter.GetCustomAttribute<ParamNamespace.BarAttribute> ();
+ Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ());
+ }
+
+ [Test]
+ public void MultipleParameterAttributes2 ()
+ {
+ var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar");
+ var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
+ Assert.IsNull (foo);
+ }
+
+ [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
+ public class MyCAttr : Attribute {}
+
+ class Base {
+ [MyCAttr]
+ public override string ToString () { return null; }
+ }
+
+ class Derived : Base {
+ public override string ToString () { return null; }
+ }
+
+ [Test] //one ton of bugs
+ public void GetCustomAttributesOnMethodOverride ()
+ {
+ var m = typeof (Derived).GetMethod ("ToString");
+ var attrs = Attribute.GetCustomAttributes (m, true);
+ Assert.AreEqual (1, attrs.Length);
+ }
+
+ class EvtBase
+ {
+ public virtual event EventHandler Event {add {} remove {}}
+ }
+
+ class EvtOverride : EvtBase
+ {
+ [MyCAttr]
+ public override event EventHandler Event {add {} remove {}}
+ }
+
+ class EvtChild : EvtOverride
+ {
+ public override event EventHandler Event {add {} remove {}}
+ }
+
+ [Test] //Regression test for #662867
+ public void GetCustomAttributesOnEventOverride ()
+ {
+ var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
+ Assert.AreEqual (1, attrs.Length);
+ }
}
}