2 // System.Reflection.ConstructorInfo Test Cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2007 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Reflection;
31 using System.Runtime.Serialization;
33 using NUnit.Framework;
35 namespace MonoTests.System.Reflection
38 public class ConstructorInfoTest
43 ConstructorInfo ctor = typeof (Foo).GetConstructor (
44 BindingFlags.Public | BindingFlags.Instance, null,
45 Type.EmptyTypes, null);
46 Foo foo = ctor.Invoke (new object [0]) as Foo;
47 Assert.IsNotNull (foo, "#1");
51 public void InvokeAbstract ()
53 object obj = FormatterServices.GetUninitializedObject (typeof (Foo));
54 ConstructorInfo ctor = typeof (AbstractFoo).GetConstructor (
55 BindingFlags.Instance | BindingFlags.NonPublic, null,
56 Type.EmptyTypes, null);
57 Assert.IsNotNull (ctor, "#A1");
58 object ret = ctor.Invoke (obj, new object [0]);
59 Assert.IsNull (ret, "#A2");
62 ctor.Invoke (null, new object [0]);
64 } catch (TargetException ex) {
65 // Non-static method requires a target
66 Assert.AreEqual (typeof (TargetException), ex.GetType (), "#B2");
67 Assert.IsNull (ex.InnerException, "#B3");
68 Assert.IsNotNull (ex.Message, "#B4");
72 ctor.Invoke (new object [0]);
74 } catch (MemberAccessException ex) {
75 // Cannot create an abstract class
76 Assert.AreEqual (typeof (MemberAccessException), ex.GetType (), "#C2");
77 Assert.IsNull (ex.InnerException, "#C3");
78 Assert.IsNotNull (ex.Message, "#C4");
83 public void InvokeOptionalArguments ()
85 var constructor = typeof (Optional).GetConstructors () [0];
87 constructor.Invoke (BindingFlags.Default, null, null, null);
89 } catch (TargetParameterCountException) {
92 object[] parameters = new [] { Type.Missing, Type.Missing, Type.Missing };
93 var instance = constructor.Invoke (BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, parameters, null);
94 Assert.IsNotNull (instance, "#2a");
97 abstract class AbstractFoo
101 class Foo : AbstractFoo
106 [ExpectedException (typeof (MemberAccessException))]
107 public void InvokeOpenGenericType () {
108 typeof (Gen<>).GetConstructor (Type.EmptyTypes).Invoke (null);
111 public class Gen<T> {
118 public Optional (string caption = null, string value = null, string layoutName = null)
124 [ExpectedException (typeof (TargetException))]
125 public void InvokeWithNullTarget ()
127 typeof (Foo).GetConstructors ()[0].Invoke (null, BindingFlags.Default, null, null, null);
131 [ExpectedException (typeof (TargetException))]
132 public void InvokeWithWrongTarget ()
134 typeof (Foo).GetConstructors ()[0].Invoke (new object (), BindingFlags.Default, null, null, null);
138 public void ContainsGenericParametersOnGenericType ()
140 var ctor = typeof (Gen<>).GetConstructor (Type.EmptyTypes);
141 Assert.IsTrue (ctor.ContainsGenericParameters);