cs-20.cs 849 B

1234567891011121314151617181920212223242526272829303132333435
  1. // cs-20.cs : Cannot find attribute type My (maybe you forgot to set the usage using the AttributeUsage attribute ?).
  2. // Line : 18
  3. using System;
  4. using System.Reflection;
  5. namespace Test {
  6. //[AttributeUsage (AttributeTargets.All)]
  7. public class MyAttribute: Attribute {
  8. public string val;
  9. public MyAttribute (string stuff) {
  10. System.Console.WriteLine (stuff);
  11. val = stuff;
  12. }
  13. }
  14. [My("testclass")]
  15. public class Test {
  16. static public int Main() {
  17. System.Reflection.MemberInfo info = typeof (Test);
  18. object[] attributes = info.GetCustomAttributes (false);
  19. for (int i = 0; i < attributes.Length; i ++) {
  20. System.Console.WriteLine(attributes[i]);
  21. }
  22. if (attributes.Length != 2)
  23. return 1;
  24. MyAttribute attr = (MyAttribute) attributes [0];
  25. if (attr.val != "testclass")
  26. return 2;
  27. return 0;
  28. }
  29. }
  30. }