2 using System.Collections.Generic;
4 using System.Reflection;
5 using Microsoft.CSharp.RuntimeBinder;
7 // Dynamic member lookup tests
10 delegate void D2 (decimal d);
14 internal int IntValue = 5;
15 internal string StringStatic = "hi";
17 public const decimal Decimal = -0.3m;
23 public Class (sbyte extra)
29 protected internal uint this[byte i] {
54 internal string Method (string value)
59 public int Method (int a, byte b)
64 public void MethodInOut (ref int refValue, out string outValue)
70 public static void GenericVoid<T> (T i)
74 public static int StaticMethod (params int[] i)
76 return i [0] * i.Length;
79 public static void ArglistMethod (__arglist)
86 internal event Func<int> OutEvent;
88 public int CallEvent ()
96 static void Assert<T> (T expected, T value, string name)
98 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
100 throw new ApplicationException (name + expected + " != " + value);
104 static void AssertError (Action expected, string name)
108 throw new ApplicationException (name + ": RuntimeBinderException expected");
109 } catch (RuntimeBinderException) {
118 #pragma warning disable 169
122 dynamic d = new[] { 5, 8, 2 };
123 Assert (8, d[1], "#1");
125 d = new int[,] { { 1, 2 }, { 3, 4 } };
126 Assert (3, d[1, 0], "#2");
128 dynamic d2 = new Class ();
129 Assert<uint> (154, d2[2], "#3");
130 Assert<uint> (154, d2[i:2], "#3a");
133 void GetIndexError_Null ()
136 AssertError (() => { var v = d[1]; }, "#1");
141 Func<string, string> f = new Class().Method;
143 Assert ("bar", d ("bar"), "#1");
145 Action<bool> f2 = Class.GenericVoid;
149 Func<string, int> f3 = (s) => 3;
156 dynamic d = new Class ();
157 Assert ("vv", d.Method ("vv"), "#1");
160 Assert (6, d.Method (b: b++, a: b), "#1a");
164 d.MethodInOut (ref arg1, out arg2);
167 Assert (2, Class.StaticMethod (d), "#2");
168 Class.StaticMethod (d);
171 void InvokeMember_Error ()
175 Class.ArglistMethod (d_arg);
179 void InvokeConstructor ()
181 dynamic d = (sbyte) 8;
182 var r = new Class (d);
183 Assert (8, r.IntValue, "#1");
185 D2 method = (decimal e) => { };
193 d.e += new Func<int> (() => 3);
194 Assert (3, d.e (), "#1");
197 Assert (5, d.field, "#2");
199 int r = d.field += 7;
200 Assert (12, r, "#2a");
201 Assert (12, d.field, "#2b");
203 d = new EventClass ();
204 d.OutEvent += new Func<int> (() => 100);
205 Assert (100, d.CallEvent (), "#3");
208 void MemberGetTest ()
210 dynamic d = new Class ();
211 Assert (5, d.IntValue, "#1");
212 Assert (180, d.Prop, "#1a");
214 Assert ("hi", d.StringStatic, "#2");
217 Assert (4, d.Length, "#3");
220 void MemberGetError_Null ()
223 AssertError (() => { var v = d.Foo; }, "#1");
226 void MemberSetTest ()
228 dynamic d = new Class ();
231 Assert (22, d.IntValue, "#1");
233 Assert (19, d.Prop, "#1a");
235 d.Prop = byte.MaxValue;
236 Assert (byte.MaxValue, d.Prop++, "#1b");
237 Assert (1, ++d.Prop, "#1c");
239 Assert (2, d.Prop, "#1d");
242 Assert (7, d.Prop, "#1e");
244 d.StringStatic = "no";
245 Assert ("no", d.StringStatic, "#2");
247 var r = d.FixedValue = 44;
248 Assert (44, r, "#3");
251 void MemberSetError_Null ()
254 AssertError (() => { d.Fo1 = 1; }, "#1");
259 dynamic d = new[] { "b", "v" };
261 Assert ("c", d[1], "#1");
263 d = new int[,] { { 1, 2 }, { 3, 4 } };
265 Assert (100, d[1, 0], "#2");
267 Assert (101, d[1, 0], "#2a");
269 d [0, 0] = d [1, 0] = 55;
270 Assert (55, d [0, 0], "#2a");
272 dynamic d2 = new Class ();
274 Assert<uint> (1000, d2[2], "#3");
276 Assert<uint> (2002, d2[2], "#3a");
278 Assert<uint> (3, d2[1], "#3b");
280 uint r = d2 [1] = 200;
281 Assert<uint> (200, r, "#4");
284 void SetIndexError_Null ()
287 AssertError (() => { d [1] = 0; }, "#1");
290 #pragma warning restore 169
292 static bool RunTest (MethodInfo test)
294 Console.Write ("Running test {0, -25}", test.Name);
296 test.Invoke (new Tester (), null);
297 Console.WriteLine ("OK");
299 } catch (Exception e) {
300 Console.WriteLine ("FAILED");
301 Console.WriteLine (e.ToString ());
302 // Console.WriteLine (e.InnerException.Message);
307 public static int Main ()
309 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
310 where test.GetParameters ().Length == 0
312 select RunTest (test);
314 int failures = tests.Count (a => !a);
315 Console.WriteLine (failures + " tests failed");