2 // System.Reflection.BinderTests - Tests Type.DefaultBinder
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
10 using NUnit.Framework;
13 using System.Reflection;
15 namespace MonoTests.System.Reflection
24 public static void SampleMethod (object o) { }
26 public Type this[decimal i] {
27 get { return i.GetType (); }
30 public Type this[object i] {
31 get { return i.GetType (); }
37 public Type this [int i] {
38 get { return i.GetType (); }
44 public Type this[byte i] {
45 get { return i.GetType (); }
48 public Type this[sbyte i] {
49 get { return i.GetType (); }
52 public Type this[short i] {
53 get { return i.GetType (); }
56 public Type this[ushort i] {
57 get { return i.GetType (); }
60 public Type this[int i] {
61 get { return i.GetType (); }
64 public Type this[uint i] {
65 get { return i.GetType (); }
68 public Type this[long i] {
69 get { return i.GetType (); }
72 public Type this[ulong i] {
73 get { return i.GetType (); }
76 public Type this[float i] {
77 get { return i.GetType (); }
80 public Type this[double i] {
81 get { return i.GetType (); }
84 public Type this[decimal i] {
85 get { return i.GetType (); }
88 public Type this[object i] {
89 get { return i.GetType (); }
92 public Type this[Enum i] {
93 get { return i.GetType (); }
98 public class BinderTest
100 Binder binder = Type.DefaultBinder;
103 [ExpectedException (typeof (ArgumentException))]
104 public void SelectPropertyTestNull1 ()
106 // The second argument is the one
107 binder.SelectProperty (0, null, null, null, null);
111 [ExpectedException (typeof (ArgumentException))]
112 public void SelectPropertyTestEmpty ()
114 // The second argument is the one
115 binder.SelectProperty (0, new PropertyInfo [] {}, null, null, null);
119 [ExpectedException (typeof (AmbiguousMatchException))]
120 public void AmbiguousProperty1 () // Bug 58381
122 Type type = typeof (MultiIndexer);
123 PropertyInfo pi = type.GetProperty ("Item");
127 public void SelectAndInvokeAllProperties1 ()
129 Type type = typeof (MultiIndexer);
130 PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
131 BindingFlags.Public |
132 BindingFlags.Instance);
134 // These don't cause an AmbiguousMatchException
135 Type [] types = { typeof (byte), typeof (short),
136 typeof (int), typeof (long),
139 /* MS matches short for sbyte!!! */
140 /* MS matches int for ushort!!! */
141 /* MS matches long for uint!!! */
142 /** These do weird things under MS if used together and then in separate arrays *
143 Type [] types = { typeof (ulong), typeof (float), typeof (double),
144 typeof (decimal), typeof (object) };
147 MultiIndexer obj = new MultiIndexer ();
149 foreach (Type t in types) {
150 PropertyInfo prop = null;
152 prop = binder.SelectProperty (0, props, null, new Type [] {t}, null);
153 } catch (Exception e) {
154 throw new Exception ("Type: " + t, e);
156 Type gotten = (Type) prop.GetValue (obj, new object [] {Activator.CreateInstance (t)});
157 Assert.AreEqual (t, gotten);
162 public void SelectAndInvokeAllProperties2 ()
164 Type type = typeof (MultiIndexer);
165 PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
166 BindingFlags.Public |
167 BindingFlags.Instance);
169 Type [] types = { typeof (ushort), typeof (char) };
171 MultiIndexer obj = new MultiIndexer ();
172 PropertyInfo prop1 = binder.SelectProperty (0, props, null, new Type [] {types [0]}, null);
173 PropertyInfo prop2 = binder.SelectProperty (0, props, null, new Type [] {types [1]}, null);
174 Assert.AreEqual (prop1, prop2);
178 public void Select1Match2 ()
180 Type type = typeof (SingleIndexer);
181 PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
182 BindingFlags.Public |
183 BindingFlags.Instance);
184 PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [0], null);
185 Assert.IsNull (prop, "empty");
189 public void Select1Match ()
191 Type type = typeof (SingleIndexer);
192 PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
193 BindingFlags.Public |
194 BindingFlags.Instance);
198 prop = binder.SelectProperty (0, props, null, new Type [] { typeof (long) }, null);
199 Assert.IsNull (prop, "long");
200 prop = binder.SelectProperty (0, props, null, new Type [] { typeof (int) }, null);
201 Assert.IsNotNull (prop, "int");
202 prop = binder.SelectProperty (0, props, null, new Type [] { typeof (short) }, null);
203 Assert.IsNotNull (prop, "short");
207 public void ArgNullOnMethod () // see bug 58846. We throwed nullref here.
209 Type type = typeof (SampleClass);
210 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod;
211 type.InvokeMember ("SampleMethod", flags, null, null, new object[] { null });
215 public void ArgNullOnProperty ()
217 Type type = typeof (SampleClass);
218 PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
219 BindingFlags.Public |
220 BindingFlags.Instance);
222 PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [] {null}, null);
223 Assert.IsNotNull (prop);
227 public void BindToMethodNamedArgs ()
229 Type t = typeof (Bug41691);
231 StringWriter sw = new StringWriter ();
234 object[] argValues = new object [] {"Hello", "World", "Extra", sw};
235 string [] argNames = new string [] {"firstName", "lastName"};
237 t.InvokeMember ("PrintName",
238 BindingFlags.InvokeMethod,
246 Assert.AreEqual ("Hello\nExtra\nWorld\n", sw.ToString ());
249 public class Bug41691
251 public static void PrintName (string lastName, string firstName, string extra, TextWriter output)
253 output.WriteLine (firstName);
254 output.WriteLine (extra);
255 output.WriteLine (lastName);
260 public void GetMethodAmbiguity ()
262 object IntegerObject = 5;
263 object IntArrayObject = new int[] {5, 2, 5};
264 object StringArrayObject = new string [] {"One", "Two"};
265 object [] IntParam = new object [] {IntegerObject};
266 object [] IntArrayParam = new object [] {IntArrayObject};
267 object [] StringArrayParam = new object [] {StringArrayObject};
270 Type betype = this.GetType ();
272 string name1 = "Bug42457Method";
273 string name2 = "Bug42457Method2";
275 MethodInfo mi_obj = betype.GetMethod (name1, Type.GetTypeArray (IntParam));
276 mi_obj.Invoke (be, IntParam);
277 Assert.AreEqual (1, bug42457, "#1");
278 MethodInfo mi_arr = betype.GetMethod (name1, Type.GetTypeArray (IntArrayParam));
279 mi_arr.Invoke (be, IntArrayParam);
280 Assert.AreEqual (2, bug42457, "#2");
281 MethodInfo mi_str = betype.GetMethod (name1, Type.GetTypeArray (StringArrayParam));
282 mi_str.Invoke (be, StringArrayParam);
283 Assert.AreEqual (3, bug42457, "#3");
285 MethodInfo m2_obj = betype.GetMethod (name2, Type.GetTypeArray (IntParam));
286 m2_obj.Invoke (be, IntParam);
287 Assert.AreEqual (1, bug42457_2, "#4");
288 MethodInfo m2_arr = betype.GetMethod (name2, Type.GetTypeArray (IntArrayParam));
289 m2_arr.Invoke (be, IntArrayParam);
290 Assert.AreEqual (2, bug42457_2, "#5");
291 MethodInfo m2_str = betype.GetMethod (name2, Type.GetTypeArray(StringArrayParam));
292 m2_str.Invoke (be, StringArrayParam);
293 Assert.AreEqual (3, bug42457_2, "#6");
296 static void MethodWithLongParam(long param)
301 public void TestExactBinding ()
303 Type[] types = new Type[] { typeof(int) };
304 Assert.AreEqual (null, typeof (BinderTest).GetMethod("MethodWithLongParam", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.ExactBinding, null, types, null));
307 public void Bug42457Method (object thing)
312 public void Bug42457Method (Array thing)
317 public void Bug42457Method (string [] thing)
322 public void Bug42457Method2 (object thing)
327 public void Bug42457Method2 (Array thing)
332 public void Bug42457Method2 (string [] thing)
337 int bug42457, bug42457_2;
340 public void GetMethodAvoidAmbiguity2 ()
342 Type tType = this.GetType ();
343 Bug77079A a = new Bug77079C ();
345 tType.InvokeMember ("Bug77079",
346 BindingFlags.Public | BindingFlags.InvokeMethod |
347 BindingFlags.Instance,
348 null, this, new object[] {a});
349 Assert.AreEqual (2, bug77079);
354 public void Bug77079 (Bug77079A a)
359 public void Bug77079 (Bug77079B a)
364 public class Bug77079A
368 public class Bug77079B : Bug77079A
372 public class Bug77079C : Bug77079B