2 using System
.Collections
.Generic
;
4 using System
.Reflection
;
5 using Microsoft
.CSharp
.RuntimeBinder
;
7 // Dynamic member lookup tests
13 internal int IntValue
= 5;
14 internal string StringStatic
= "hi";
16 public const decimal Decimal
= -0.3m
;
19 protected internal uint this[byte i
] {
44 internal string Method (string value)
49 public static void GenericVoid
<T
> (T i
)
53 public static int StaticMethod (params int[] i
)
55 return i
[0] * i
.Length
;
61 delegate void EmptyDelegate ();
62 delegate int IntDelegate ();
64 static void Assert
<T
> (T expected
, T
value, string name
)
66 if (!EqualityComparer
<T
>.Default
.Equals (expected
, value)) {
68 throw new ApplicationException (name
+ expected
+ " != " + value);
72 static void AssertError (Action expected
, string name
)
76 throw new ApplicationException (name
+ ": RuntimeBinderException expected");
77 } catch (RuntimeBinderException
) {
82 #pragma warning disable 169
86 dynamic d
= new[] { 5, 8, 2 }
;
87 Assert (8, d
[1], "#1");
89 d
= new int[,] { { 1, 2 }
, { 3, 4 }
};
90 Assert (3, d
[1, 0], "#2");
92 dynamic d2
= new Class ();
93 Assert
<uint> (154, d2
[2], "#3");
94 Assert
<uint> (154, d2
[i
:2], "#3a");
97 void GetIndexError_Null ()
100 AssertError (() => { var v = d[1]; }
, "#1");
105 Func
<string, string> f
= new Class().Method
;
107 Assert ("bar", d ("bar"), "#1");
109 Action
<bool> f2
= Class
.GenericVoid
;
111 Assert
<object> (null, d (true), "#2");
116 // dynamic d = new Class ();
117 // Assert ("vv", d.Method ("vv"), "#1");
120 Assert (2, Class
.StaticMethod (d
), "#2");
123 void MemberGetTest ()
125 dynamic d
= new Class ();
126 Assert (5, d
.IntValue
, "#1");
127 Assert (180, d
.Prop
, "#1a");
129 Assert ("hi", d
.StringStatic
, "#2");
132 Assert (4, d
.Length
, "#3");
134 // d.Event += delegate () { }; CS0019
137 void MemberGetError_Null ()
140 AssertError (() => { var v = d.Foo; }
, "#1");
143 void MemberSetTest ()
145 dynamic d
= new Class ();
148 Assert (22, d
.IntValue
, "#1");
150 Assert (19, d
.Prop
, "#1a");
152 d
.Prop
= byte.MaxValue
;
154 Assert (0, d
.Prop
, "#1b");
156 d
.StringStatic
= "no";
157 Assert ("no", d
.StringStatic
, "#2");
159 var r
= d
.FixedValue
= 44;
160 Assert (44, r
, "#3");
163 void MemberSetError_Null ()
166 AssertError (() => { d.Fo1 = 1; }
, "#1");
171 dynamic d
= new[] { "b", "v" }
;
173 Assert ("c", d
[1], "#1");
175 d
= new int[,] { { 1, 2 }
, { 3, 4 }
};
177 Assert (100, d
[1, 0], "#2");
179 Assert (101, d
[1, 0], "#2a");
181 d
[0, 0] = d
[1, 0] = 55;
182 Assert (55, d
[0, 0], "#2a");
184 dynamic d2
= new Class ();
186 Assert
<uint> (1000, d2
[2], "#3");
188 Assert
<uint> (2002, d2
[2], "#3a");
190 Assert
<uint> (3, d2
[1], "#3b");
192 uint r
= d2
[1] = 200;
193 Assert
<uint> (200, r
, "#4");
196 void SetIndexError_Null ()
199 AssertError (() => { d [1] = 0; }
, "#1");
202 #pragma warning restore 169
204 static bool RunTest (MethodInfo test
)
206 Console
.Write ("Running test {0, -25}", test
.Name
);
208 test
.Invoke (new Tester (), null);
209 Console
.WriteLine ("OK");
211 } catch (Exception e
) {
212 Console
.WriteLine ("FAILED");
213 Console
.WriteLine (e
.ToString ());
214 // Console.WriteLine (e.InnerException.Message);
219 public static int Main ()
221 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
222 where test
.GetParameters ().Length
== 0
224 select RunTest (test
);
226 int failures
= tests
.Count (a
=> !a
);
227 Console
.WriteLine (failures
+ " tests failed");