CustomAttributeDataTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Reflection.CustomAttributeData Test Cases
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. //
  7. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  8. // Copyright (C) 2004-2008 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Reflection;
  32. using System.Collections.Generic;
  33. using System.Runtime.InteropServices;
  34. namespace MonoTests.System.Reflection
  35. {
  36. class Attr : Attribute {
  37. public Attr (byte[] arr) {
  38. }
  39. }
  40. [TestFixture]
  41. public class CustomAttributeDataTest
  42. {
  43. [MarshalAs (UnmanagedType.LPStr)]
  44. [NonSerialized]
  45. public string fieldDecoratedWithPseudoCustomAttributes = "test";
  46. [Attr (new byte [] { 1, 2 })]
  47. public void MethodWithAttr () {
  48. }
  49. public void MethodWithParamDecoratedWithPseudoCustomAttributes ([Optional, In, Out, MarshalAs (UnmanagedType.LPStr)] String s)
  50. {
  51. }
  52. [return: MarshalAs (UnmanagedType.LPStr)]
  53. public string MethodWithReturnValueDecoratedWithMarshalAs ()
  54. {
  55. return "test";
  56. }
  57. [Test]
  58. [Category ("MobileNotWorking")] // #10263
  59. public void Arrays () {
  60. IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod ("MethodWithAttr"));
  61. Assert.AreEqual (1, cdata.Count);
  62. CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
  63. Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()));
  64. IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
  65. Assert.AreEqual (2, arr.Count);
  66. Assert.AreEqual (typeof (byte), arr [0].ArgumentType);
  67. Assert.AreEqual (1, arr [0].Value);
  68. Assert.AreEqual (typeof (byte), arr [1].ArgumentType);
  69. Assert.AreEqual (2, arr [1].Value);
  70. }
  71. [Test]
  72. public void ParameterIncludesPseudoCustomAttributesData ()
  73. {
  74. var methodInfo = typeof (CustomAttributeDataTest).GetMethod ("MethodWithParamDecoratedWithPseudoCustomAttributes");
  75. var paramInfo = methodInfo.GetParameters () [0];
  76. var customAttributesData = CustomAttributeData.GetCustomAttributes (paramInfo);
  77. Assert.AreEqual (4, customAttributesData.Count);
  78. var inAttributeData = customAttributesData [0];
  79. var optionalAttributeData = customAttributesData [1];
  80. var outAttributeData = customAttributesData [2];
  81. var marshalAsAttributeData = customAttributesData [3];
  82. var marshalAsAttributeCtorArg = marshalAsAttributeData.ConstructorArguments [0];
  83. Assert.AreEqual (typeof (InAttribute), inAttributeData.AttributeType);
  84. Assert.AreEqual (typeof (OptionalAttribute), optionalAttributeData.AttributeType);
  85. Assert.AreEqual (typeof (OutAttribute), outAttributeData.AttributeType);
  86. Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
  87. Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeCtorArg.ArgumentType);
  88. Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeCtorArg.Value);
  89. }
  90. [Test]
  91. public void FieldIncludesPseudoCustomAttributesData ()
  92. {
  93. var fieldInfo = typeof (CustomAttributeDataTest).GetField ("fieldDecoratedWithPseudoCustomAttributes");
  94. var customAttributesData = CustomAttributeData.GetCustomAttributes (fieldInfo);
  95. Assert.AreEqual (2, customAttributesData.Count);
  96. var nonSerializedAttributeData = customAttributesData [0];
  97. var marshalAsAttributeData = customAttributesData [1];
  98. var marshalAsAttributeDataCtorArg = marshalAsAttributeData.ConstructorArguments [0];
  99. Assert.AreEqual (typeof (NonSerializedAttribute), nonSerializedAttributeData.AttributeType);
  100. Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
  101. Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeDataCtorArg.ArgumentType);
  102. Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeDataCtorArg.Value);
  103. }
  104. [Test]
  105. public void MethodIncludesMarshalAsAttributeData ()
  106. {
  107. var methodInfo = typeof (CustomAttributeDataTest).GetMethod ("MethodWithReturnValueDecoratedWithMarshalAs");
  108. var paramInfo = (ParameterInfo)methodInfo.ReturnTypeCustomAttributes;
  109. var customAttributesData = CustomAttributeData.GetCustomAttributes (paramInfo);
  110. var marshalAsAttributeData = customAttributesData [0];
  111. var ctorArg = marshalAsAttributeData.ConstructorArguments [0];
  112. Assert.AreEqual (1, customAttributesData.Count);
  113. Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
  114. Assert.AreEqual (typeof (UnmanagedType), ctorArg.ArgumentType);
  115. Assert.AreEqual ((int)UnmanagedType.LPStr, ctorArg.Value);
  116. }
  117. }
  118. }