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
;
82 static void Assert
<T
> (T expected
, T
value, string name
)
84 if (!EqualityComparer
<T
>.Default
.Equals (expected
, value)) {
86 throw new ApplicationException (name
+ expected
+ " != " + value);
90 static void AssertError (Action expected
, string name
)
94 throw new ApplicationException (name
+ ": RuntimeBinderException expected");
95 } catch (RuntimeBinderException
) {
100 #pragma warning disable 169
104 dynamic d
= new[] { 5, 8, 2 }
;
105 Assert (8, d
[1], "#1");
107 d
= new int[,] { { 1, 2 }
, { 3, 4 }
};
108 Assert (3, d
[1, 0], "#2");
110 dynamic d2
= new Class ();
111 Assert
<uint> (154, d2
[2], "#3");
112 Assert
<uint> (154, d2
[i
:2], "#3a");
115 void GetIndexError_Null ()
118 AssertError (() => { var v = d[1]; }
, "#1");
123 Func
<string, string> f
= new Class().Method
;
125 Assert ("bar", d ("bar"), "#1");
127 Action
<bool> f2
= Class
.GenericVoid
;
131 Func
<string, int> f3
= (s
) => 3;
138 dynamic d
= new Class ();
139 Assert ("vv", d
.Method ("vv"), "#1");
142 Assert (6, d
.Method (b
: b
++, a
: b
), "#1a");
146 d
.MethodInOut (ref arg1
, out arg2
);
149 Assert (2, Class
.StaticMethod (d
), "#2");
150 Class
.StaticMethod (d
);
153 void InvokeConstructor ()
155 dynamic d
= (sbyte) 8;
156 var r
= new Class (d
);
157 Assert (8, r
.IntValue
, "#1");
159 D2 method
= (decimal e
) => { }
;
169 d
.e
+= new Func
<int> (() => 3);
172 //Assert (3, d.e (), "#1");
175 Assert (5, d
.field
, "#2");
178 void MemberGetTest ()
180 dynamic d
= new Class ();
181 Assert (5, d
.IntValue
, "#1");
182 Assert (180, d
.Prop
, "#1a");
184 Assert ("hi", d
.StringStatic
, "#2");
187 Assert (4, d
.Length
, "#3");
190 void MemberGetError_Null ()
193 AssertError (() => { var v = d.Foo; }
, "#1");
196 void MemberSetTest ()
198 dynamic d
= new Class ();
201 Assert (22, d
.IntValue
, "#1");
203 Assert (19, d
.Prop
, "#1a");
205 d
.Prop
= byte.MaxValue
;
206 Assert (byte.MaxValue
, d
.Prop
++, "#1b");
207 Assert (1, ++d
.Prop
, "#1c");
209 Assert (2, d
.Prop
, "#1d");
212 Assert (7, d
.Prop
, "#1e");
214 d
.StringStatic
= "no";
215 Assert ("no", d
.StringStatic
, "#2");
217 var r
= d
.FixedValue
= 44;
218 Assert (44, r
, "#3");
221 void MemberSetError_Null ()
224 AssertError (() => { d.Fo1 = 1; }
, "#1");
229 dynamic d
= new[] { "b", "v" }
;
231 Assert ("c", d
[1], "#1");
233 d
= new int[,] { { 1, 2 }
, { 3, 4 }
};
235 Assert (100, d
[1, 0], "#2");
237 Assert (101, d
[1, 0], "#2a");
239 d
[0, 0] = d
[1, 0] = 55;
240 Assert (55, d
[0, 0], "#2a");
242 dynamic d2
= new Class ();
244 Assert
<uint> (1000, d2
[2], "#3");
246 Assert
<uint> (2002, d2
[2], "#3a");
248 Assert
<uint> (3, d2
[1], "#3b");
250 uint r
= d2
[1] = 200;
251 Assert
<uint> (200, r
, "#4");
254 void SetIndexError_Null ()
257 AssertError (() => { d [1] = 0; }
, "#1");
260 #pragma warning restore 169
262 static bool RunTest (MethodInfo test
)
264 Console
.Write ("Running test {0, -25}", test
.Name
);
266 test
.Invoke (new Tester (), null);
267 Console
.WriteLine ("OK");
269 } catch (Exception e
) {
270 Console
.WriteLine ("FAILED");
271 Console
.WriteLine (e
.ToString ());
272 // Console.WriteLine (e.InnerException.Message);
277 public static int Main ()
279 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
280 where test
.GetParameters ().Length
== 0
282 select RunTest (test
);
284 int failures
= tests
.Count (a
=> !a
);
285 Console
.WriteLine (failures
+ " tests failed");