CustomAttributeBuilderTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // CustomAttributeBuilderTest.cs
  2. //
  3. // Author: Vineeth N <[email protected]>
  4. //
  5. // (C) 2004 Ximian, Inc. http://www.ximian.com
  6. //
  7. using System;
  8. using System.Reflection;
  9. using System.Reflection.Emit;
  10. using System.Threading;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Reflection.Emit
  13. {
  14. /// <summary>
  15. /// TestFixture for CustomAttributeBuilderTest.
  16. /// The members to be tested are as follows:
  17. /// 4 constructors:
  18. /// 1) public CustomAttributeBuilder(ConstructorInfo, object[]);
  19. /// 2) public CustomAttributeBuilder(ConstructorInfo, object[], FieldInfo[], object[]);
  20. /// 3) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[]);
  21. /// 4) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
  22. /// and the exceptions that are thrown.
  23. /// In the implementation , it can be seen that the first
  24. /// three type of constructors call the 4th type of ctor, which takes 6 args
  25. /// by filling args and substituting null as required.
  26. /// For testing constructors we have use 4 different test functions,
  27. /// Various exceptions have been checked for 4th type of consturctor.
  28. /// </summary>
  29. [TestFixture]
  30. public class CustomAttributeBuilderTest : Assertion
  31. {
  32. // the CustomAttribute class is used for testing and it has to be public
  33. //since it will be associated with a class that belongs to another assembly
  34. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Class)]
  35. public class CustomAttribute: Attribute
  36. {
  37. private string attr1;
  38. private string attr2;
  39. public string Feild; //used for testing the second type of constructor
  40. public CustomAttribute () {}
  41. public CustomAttribute (String s1 , String s2)
  42. {
  43. attr1 = s1;
  44. attr2=s2;
  45. }
  46. private CustomAttribute (String s1) {}
  47. static CustomAttribute () {}
  48. public string AttributeOne
  49. {
  50. get { return attr1; }
  51. set { attr1 = value; }
  52. }
  53. public string AttributeTwo
  54. {
  55. get { return attr2; }
  56. //the set is skipped and is used later in testing
  57. }
  58. }
  59. private class TempClass
  60. {
  61. //used for testing the ArgumentException
  62. public string Field;
  63. public string FieldProperty
  64. {
  65. get { return Field; }
  66. set { Field = value; }
  67. }
  68. }
  69. [Test]
  70. public void CtorOneTest ()
  71. {
  72. //test for the constructor with signature--
  73. // public CustomAttributeBuilder(ConstructorInfo, object[]);
  74. /*
  75. * WE build a imaginary type as follows
  76. * class TestType
  77. * {
  78. * [CustomAttribute("one","two")]
  79. * public string Str;
  80. *
  81. * [CustomAttribute("hello","world")]
  82. * public void Print()
  83. * {Console.WriteLine("Hello World"); }
  84. *
  85. * }
  86. * And then check for the validity of attributes in the test functions
  87. */
  88. AssemblyName asmName = new AssemblyName ();
  89. asmName.Name = "TestAssembly.dll";
  90. AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
  91. asmName , AssemblyBuilderAccess.Run);
  92. ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
  93. TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
  94. TypeAttributes.Public);
  95. Type[] ctorParams = new Type[] { typeof (string),typeof (string) };
  96. ConstructorInfo classCtorInfo =
  97. typeof (CustomAttribute).GetConstructor (ctorParams);
  98. CustomAttributeBuilder feildCABuilder = new CustomAttributeBuilder (
  99. classCtorInfo,
  100. new object [] { "one","two" }
  101. ),
  102. methodCABuilder = new CustomAttributeBuilder (
  103. classCtorInfo,
  104. new object [] { "hello","world" }
  105. );
  106. //now let's build a feild of type string and associate a attribute with it
  107. FieldBuilder fieldBuilder= typeBuilder.DefineField ("Str",
  108. typeof (string), FieldAttributes.Public);
  109. fieldBuilder.SetCustomAttribute (feildCABuilder);
  110. //now build a method
  111. MethodBuilder methodBuilder= typeBuilder.DefineMethod ("Print",
  112. MethodAttributes.Public, null, null);
  113. methodBuilder.SetCustomAttribute (methodCABuilder);
  114. ILGenerator methodIL = methodBuilder.GetILGenerator ();
  115. methodIL.EmitWriteLine ("Hello, world!");
  116. methodIL.Emit (OpCodes.Ret);
  117. // create the type
  118. Type myType = typeBuilder.CreateType ();
  119. //Now check for the validity of the attributes.
  120. object testInstance = Activator.CreateInstance (myType);
  121. //check the validity of the attribute associated with Print method
  122. object [] methodAttrs = myType.GetMember ("Print") [0].GetCustomAttributes (true);
  123. AssertEquals ("#method Print has exactly one attribute", methodAttrs.Length, 1);
  124. CustomAttribute methodAttr = methodAttrs [0] as CustomAttribute;
  125. AssertEquals ("#AttributeOne", methodAttr.AttributeOne, "hello");
  126. AssertEquals ("#AttributeTwo", methodAttr.AttributeTwo, "world");
  127. //check the validity of the attribute associated with Str feild
  128. object [] fieldAttrs = myType.GetField ("Str").GetCustomAttributes (true);
  129. AssertEquals("#feild Str has exactly one attribute", fieldAttrs.Length, 1);
  130. CustomAttribute fieldAttr = fieldAttrs [0] as CustomAttribute;
  131. AssertEquals("#AttributeOne", fieldAttr.AttributeOne, "one");
  132. AssertEquals("#AttributeTwo", fieldAttr.AttributeTwo, "two");
  133. }
  134. [Test]
  135. public void CtorTwoTest ()
  136. {
  137. //test for the constructor with signature--
  138. // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;
  139. /*
  140. * WE build a imaginary type as follows
  141. * [CustomAttribute("Test","Type")]
  142. * public class TestType
  143. * {
  144. *
  145. * }
  146. * We also set the "Feild" of class CustomAttribute and the value;
  147. * And then check for the validity of attributes in the test functions
  148. */
  149. AssemblyName asmName = new AssemblyName ();
  150. asmName.Name = "TestAssembly.dll";
  151. AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
  152. asmName, AssemblyBuilderAccess.Run);
  153. ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
  154. TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
  155. TypeAttributes.Public);
  156. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  157. ConstructorInfo classCtorInfo =
  158. typeof (CustomAttribute).GetConstructor (ctorParams);
  159. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  160. classCtorInfo,
  161. new object [] { "Test","Type" },
  162. typeof(CustomAttribute).GetFields(),
  163. new object [] { "TestCase" }
  164. );
  165. typeBuilder.SetCustomAttribute (typeCABuilder);
  166. // create the type
  167. Type myType = typeBuilder.CreateType ();
  168. //Now check for the validity of the attributes.
  169. object testInstance = Activator.CreateInstance (myType);
  170. //check the validity of the attribute associated with Print method
  171. object [] customAttrs = myType.GetCustomAttributes (false);
  172. AssertEquals ("#TestType has exactly one attribute", customAttrs.Length, 1);
  173. //Custom Attributes of TestType
  174. CustomAttribute attr = customAttrs [0] as CustomAttribute;
  175. AssertEquals ("#AttributeOne", attr.AttributeOne, "Test");
  176. AssertEquals ("#AttributeTwo", attr.AttributeTwo, "Type");
  177. AssertEquals ("#CustomAttribute.Feild",attr.Feild, "TestCase");
  178. }
  179. [Test]
  180. public void CtorThreeTest ()
  181. {
  182. //test for the constructor with signature--
  183. // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;
  184. /*
  185. * WE build a imaginary type as follows
  186. * [CustomAttribute()]
  187. * public class TestType
  188. * {
  189. *
  190. * }
  191. * We also set the "AttributeOne" of class CustomAttribute by means of the constuctor
  192. * And then check for the validity of attribute state
  193. */
  194. AssemblyName asmName = new AssemblyName ();
  195. asmName.Name = "TestAssembly.dll";
  196. AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
  197. asmName, AssemblyBuilderAccess.Run);
  198. ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
  199. TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
  200. TypeAttributes.Public);
  201. Type [] ctorParams = new Type [] { };
  202. ConstructorInfo classCtorInfo =
  203. typeof (CustomAttribute).GetConstructor (ctorParams);
  204. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  205. classCtorInfo,
  206. new object [] { },
  207. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  208. new object [] { "TestCase" }
  209. );
  210. typeBuilder.SetCustomAttribute (typeCABuilder);
  211. // create the type
  212. Type myType = typeBuilder.CreateType ();
  213. //Now check for the validity of the attributes.
  214. object testInstance = Activator.CreateInstance (myType);
  215. //check the validity of the attribute associated with Print method
  216. object [] customAttrs = myType.GetCustomAttributes (false);
  217. AssertEquals ("#TestType has exactly one attribute", customAttrs.Length , 1);
  218. //Custom Attributes of TestType
  219. CustomAttribute attr = customAttrs [0] as CustomAttribute;
  220. AssertEquals("#AttributeOne", attr.AttributeOne, "TestCase");
  221. }
  222. [Test]
  223. public void CtorFourTest ()
  224. {
  225. //test for the constructor with signature--
  226. //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
  227. /*
  228. * WE build a imaginary type as follows
  229. * [CustomAttribute()]
  230. * public class TestType
  231. * {
  232. *
  233. * }
  234. * We also set the "AttributeOne" property ,
  235. * and "Feild" of class CustomAttribute
  236. * by means of the constuctor of CustomAttributeBuilder
  237. * And then check for the validity
  238. */
  239. AssemblyName asmName = new AssemblyName ();
  240. asmName.Name = "TestAssembly.dll";
  241. AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
  242. asmName , AssemblyBuilderAccess.Run);
  243. ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
  244. TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
  245. TypeAttributes.Public);
  246. Type [] ctorParams = new Type [] { };
  247. ConstructorInfo classCtorInfo =
  248. typeof (CustomAttribute).GetConstructor (ctorParams);
  249. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder(
  250. classCtorInfo,
  251. new object [] { },
  252. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  253. new object [] { "TestCase" },
  254. typeof(CustomAttribute).GetFields (),
  255. new object [] { "FieldValue" }
  256. );
  257. typeBuilder.SetCustomAttribute (typeCABuilder);
  258. // create the type
  259. Type myType = typeBuilder.CreateType ();
  260. //Now check for the validity of the attributes.
  261. object testInstance = Activator.CreateInstance (myType);
  262. //check the validity of the attribute associated with Print method
  263. object [] customAttrs = myType.GetCustomAttributes (false);
  264. AssertEquals("#TestType has exactly one attribute",customAttrs.Length , 1);
  265. //Custom Attributes of TestType
  266. CustomAttribute attr = customAttrs [0] as CustomAttribute;
  267. AssertEquals ("#AttributeOne", attr.AttributeOne, "TestCase");
  268. AssertEquals ("#Field ", attr.Feild, "FieldValue");
  269. }
  270. [Test]
  271. [ExpectedException (typeof (ArgumentException))]
  272. public void ArgumentExceptionTest_1 ()
  273. {
  274. //here the constructor is static
  275. Type [] ctorParams = new Type [] { };
  276. ConstructorInfo classCtorInfo =
  277. typeof (CustomAttribute).GetConstructor (BindingFlags.Static | BindingFlags.NonPublic,
  278. null, ctorParams, null);
  279. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  280. classCtorInfo,
  281. new object [] { },
  282. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  283. new object [] { "TestCase" },
  284. typeof (CustomAttribute).GetFields (),
  285. new object [] { "FieldValue" }
  286. );
  287. }
  288. [Test]
  289. [ExpectedException (typeof (ArgumentException))]
  290. public void ArgumentExceptionTest_2 ()
  291. {
  292. //here the consturctor is private
  293. Type [] ctorParams = new Type[] {typeof(string) };
  294. ConstructorInfo classCtorInfo =
  295. typeof (CustomAttribute).GetConstructor (BindingFlags.Instance |
  296. BindingFlags.NonPublic, null, ctorParams, null);
  297. Assert ("#Custom Attribute has private constuctor ", classCtorInfo != null);
  298. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  299. classCtorInfo,
  300. new object [] { "hello" },
  301. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  302. new object [] { "TestCase" },
  303. typeof (CustomAttribute).GetFields (),
  304. new object [] { "FieldValue" }
  305. );
  306. }
  307. [Test]
  308. [ExpectedException (typeof (ArgumentException))]
  309. public void ArgumentExceptionTest_3 ()
  310. {
  311. // The lengths of the namedProperties and
  312. //propertyValues arrays are different.
  313. Type [] ctorParams = new Type [] { };
  314. ConstructorInfo classCtorInfo =
  315. typeof (CustomAttribute).GetConstructor (ctorParams);
  316. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  317. classCtorInfo,
  318. new object [] { },
  319. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  320. new object [] { "TestCase","extra arg" },//<--here is the error
  321. typeof (CustomAttribute).GetFields (),
  322. new object [] { "FieldValue" }
  323. );
  324. }
  325. [Test]
  326. [ExpectedException (typeof (ArgumentException))]
  327. public void ArgumentExceptionTest_4()
  328. {
  329. //The length of the namedFields and
  330. //namedValues are different
  331. Type [] ctorParams = new Type [] { };
  332. ConstructorInfo classCtorInfo =
  333. typeof (CustomAttribute).GetConstructor (ctorParams);
  334. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  335. classCtorInfo,
  336. new object [] { },
  337. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  338. new object [] { "TestCase" },
  339. typeof (CustomAttribute).GetFields (),
  340. new object [] { }//<--here is the error
  341. );
  342. }
  343. [Test]
  344. [ExpectedException (typeof (ArgumentException))]
  345. public void ArgumentExceptionTest_5 ()
  346. {
  347. //This does not throw on .NET 1.1, so manually throw
  348. if (Environment.Version.Major == 1 && Environment.Version.Major == 1)
  349. throw new ArgumentException();
  350. //The number of supplied arguments does not match
  351. //the number of parameters of the constructor as
  352. //required by the calling convention of the constructor
  353. Type [] ctorParams = new Type [] { };
  354. ConstructorInfo classCtorInfo =
  355. typeof (CustomAttribute).GetConstructor (ctorParams);
  356. /*
  357. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  358. classCtorInfo,
  359. new object [] { "extra1", "extra2" }, //<--here is the error
  360. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  361. new object [] { "TestCase" },
  362. typeof (CustomAttribute).GetFields (),
  363. new object [] { "FeildValue" }
  364. );
  365. */
  366. }
  367. [Test]
  368. [ExpectedException (typeof (ArgumentException))]
  369. public void ArgumentExceptionTest_6 ()
  370. {
  371. //The type of supplied argument does not
  372. //match the type of the parameter declared
  373. //in the constructor.
  374. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  375. ConstructorInfo classCtorInfo =
  376. typeof (CustomAttribute).GetConstructor (ctorParams);
  377. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  378. classCtorInfo,
  379. new object [] { "1", 123 },//<--here is the error,(int instead of string)
  380. new PropertyInfo[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  381. new object [] { "TestCase" },
  382. typeof (CustomAttribute).GetFields (),
  383. new object [] { "FeildValue" }
  384. );
  385. }
  386. [Test]
  387. [ExpectedException (typeof (ArgumentException))]
  388. public void ArgumentExceptionTest_7 ()
  389. {
  390. //A property has no setter.(CustomAttribute.AttributeTwo)
  391. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  392. ConstructorInfo classCtorInfo =
  393. typeof (CustomAttribute).GetConstructor (ctorParams);
  394. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  395. classCtorInfo,
  396. new object [] { "1","2" },
  397. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeTwo") },
  398. new object [] { "TestCase" },
  399. typeof (CustomAttribute).GetFields (),
  400. new object [] { "FeildValue" }
  401. );
  402. }
  403. [Test]
  404. [ExpectedException (typeof (ArgumentException))]
  405. public void ArgumentExceptionTest_8 ()
  406. {
  407. //A property doesnot belong to same class
  408. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  409. ConstructorInfo classCtorInfo =
  410. typeof (CustomAttribute).GetConstructor (ctorParams);
  411. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  412. classCtorInfo,
  413. new object [] { "1","2" },
  414. new PropertyInfo [] { typeof (TempClass).GetProperty ("FieldProperty")}, //here is the error
  415. new object [] { "TestCase" },
  416. typeof (CustomAttribute).GetFields (),
  417. new object [] { "FeildValue" }
  418. );
  419. }
  420. [Test]
  421. [ExpectedException (typeof (ArgumentException))]
  422. public void ArgumentExceptionTest_9 ()
  423. {
  424. //A field doesnot belong to same class
  425. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  426. ConstructorInfo classCtorInfo =
  427. typeof (CustomAttribute).GetConstructor (ctorParams);
  428. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  429. classCtorInfo,
  430. new object [] { "1","2" },
  431. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  432. new object [] {"TestCase"},
  433. typeof (TempClass).GetFields (), //<-- fields of TempClass are passed
  434. new object [] { "FeildValue" }
  435. );
  436. }
  437. [Test]
  438. [ExpectedException (typeof (ArgumentException))]
  439. public void ArgumentExceptionTest_10 ()
  440. {
  441. //The types of the property values do
  442. //not match the types of the named properties.
  443. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  444. ConstructorInfo classCtorInfo =
  445. typeof (CustomAttribute).GetConstructor (ctorParams);
  446. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  447. classCtorInfo,
  448. new object [] { "1","2" },
  449. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  450. new object [] { (long)1212121212 }, //<---type mismatch error(long for string)
  451. typeof (CustomAttribute).GetFields (),
  452. new object [] { "FeildValue" }
  453. );
  454. }
  455. [Test]
  456. [ExpectedException (typeof (ArgumentException))]
  457. public void ArgumentExceptionTest_11 ()
  458. {
  459. //The types of the field values do
  460. //not match the types of the named properties.
  461. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  462. ConstructorInfo classCtorInfo =
  463. typeof (CustomAttribute).GetConstructor (ctorParams);
  464. Assert ("#ctor not null", classCtorInfo != null);
  465. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  466. classCtorInfo,
  467. new object [] { "1","2" },
  468. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  469. new object [] { "One" },
  470. typeof (CustomAttribute).GetFields (),
  471. new object []{ 12.1212 } //<---type mismatch error(double for string)
  472. );
  473. }
  474. [Test]
  475. [ExpectedException (typeof (ArgumentNullException))]
  476. public void ArgumentNullException_1 ()
  477. {
  478. //the ctor value array (2nd argument) is null
  479. Type [] ctorParams = new Type [] { typeof (string),typeof (string) };
  480. ConstructorInfo classCtorInfo =
  481. typeof (CustomAttribute).GetConstructor (ctorParams);
  482. Assert ("#ctor not null", classCtorInfo != null);
  483. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  484. classCtorInfo,
  485. null, //<-- here is the error
  486. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  487. new object [] { "One" },
  488. typeof (CustomAttribute).GetFields (),
  489. new object [] { "feild" }
  490. );
  491. }
  492. [Test]
  493. [ExpectedException (typeof (ArgumentNullException))]
  494. public void ArgumentNullException_2 ()
  495. {
  496. //the property value array (4th argument) is null
  497. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  498. ConstructorInfo classCtorInfo =
  499. typeof (CustomAttribute).GetConstructor (ctorParams);
  500. Assert ("#ctor not null", classCtorInfo != null);
  501. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  502. classCtorInfo,
  503. new object [] { "one","two" },
  504. new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
  505. null, // <-- here is the error
  506. typeof (CustomAttribute).GetFields (),
  507. new object [] { "feild" }
  508. );
  509. }
  510. [Test]
  511. [ExpectedException (typeof (ArgumentNullException))]
  512. public void ArgumentNullException_3 ()
  513. {
  514. //the field value array (6th argument) is null
  515. Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
  516. ConstructorInfo classCtorInfo =
  517. typeof (CustomAttribute).GetConstructor (ctorParams);
  518. Assert ("#ctor not null", classCtorInfo != null);
  519. CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
  520. classCtorInfo,
  521. new object [] { "one","two" },
  522. new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
  523. new object [] {"property"},
  524. typeof (CustomAttribute).GetFields (),
  525. null // <-- here is the error
  526. );
  527. }
  528. }
  529. }