Updated with review feedback.
[mono.git] / mcs / tests / test-19.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4
5 class I {
6
7         public delegate string GetTextFn (string a);
8
9         static public GetTextFn GetText;
10
11         static string fn (string s)
12         {
13                 return "(" + s + ")";
14         }
15         
16         static I ()
17         {
18                 GetText = new GetTextFn (fn);
19         }
20 }
21
22 class X {
23
24         public delegate int Foo (int i, int j);
25         
26         private void Thread_func () {
27                 Console.WriteLine ("Inside the thread !");
28         }
29
30         public int Func (int i, int j)
31         {
32                 return i+j;
33         }
34
35         public void Bar ()
36         {
37                 Foo my_func = new Foo (Func);
38
39                 int result = my_func (2, 4);
40
41                 Console.WriteLine ("Answer is : " + result);
42         }
43
44         static bool MyFilter (MemberInfo mi, object criteria)
45         {
46                 Console.WriteLine ("You passed in : " + criteria);
47                 return true;
48         }
49         
50         public static int Main ()
51         {
52                 I.GetTextFn _ = I.GetText;
53
54         Console.WriteLine ("Value: " + I.GetText);
55                 X x = new X ();
56
57                 Thread thr = new Thread (new ThreadStart (x.Thread_func));
58
59                 thr.Start ();
60                 Console.WriteLine ("Inside main ");
61                 thr.Join ();
62
63                 Console.WriteLine (_("Hello"));
64
65                 x.Bar ();
66
67                 MemberFilter filter = new MemberFilter (MyFilter);
68
69                 Type t = x.GetType ();
70
71                 MemberInfo [] mi = t.FindMembers (MemberTypes.Method, BindingFlags.Static | BindingFlags.NonPublic,
72                                                   Type.FilterName, "MyFilter");
73
74                 Console.WriteLine ("FindMembers called, mi = " + mi);
75                 Console.WriteLine ("   Count: " + mi.Length);
76                 if (!filter (mi [0], "MyFilter"))
77                         return 1;
78
79                 //
80                 // This test is used to call into a delegate defined in a separate
81                 // namespace, but which is still not a nested delegate inside a class
82                 //
83                 NameSpace.TestDelegate td = new NameSpace.TestDelegate (multiply_by_three);
84
85                 if (td (8) != 24)
86                         return 30;
87
88                 //
89                 // Check the names that were used to define the delegates
90                 //
91                 if (td.GetType ().FullName != "NameSpace.TestDelegate")
92                         return 31;
93
94                 if (_.GetType ().FullName != "I+GetTextFn")
95                         return 32;
96                 
97                 Console.WriteLine ("Test passes");
98
99                 return 0;
100         }
101
102         static int multiply_by_three (int v)
103         {
104                 return v * 3;
105         }
106         
107 }
108
109 namespace NameSpace {
110
111         public delegate int TestDelegate (int a);
112
113 }
114
115
116 namespace TestNamespace
117 {
118         public class TestClass
119         {
120                 public delegate float NotWorkingDelegate(float point, params object [] hiddenParams);
121         }
122 }