2006-06-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mono / tests / cas / inheritance / reftype1.cs
1 using System;
2 using System.Reflection;
3 using System.Security;
4 using System.Security.Permissions;
5
6 public class Program {
7
8         private const string filename = "library1a";
9
10         static int GetTypeFalse (Assembly a)
11         {
12                 string typename = "InheritanceDemand";
13                 Type t = a.GetType (typename, false);
14                 if (t == null) {
15                         Console.WriteLine ("*0* Get null for type '{0}' with security.", typename);
16                         return 0;
17                 } else {
18                         Console.WriteLine ("*1* Can get type '{0}' with security (false).", typename);
19                         return 1;
20                 }
21         }
22
23         static int GetTypeTrue (Assembly a)
24         {
25                 try {
26                         string typename = "InheritanceDemand";
27                         Type t = a.GetType (typename, true);
28                         Console.WriteLine ("*1* Can get type '{0}' with security (true).", t);
29                         return 1;
30                 }
31                 catch (SecurityException se) {
32                         Console.WriteLine ("*0* Expected SecurityException\n{0}", se);
33                         return 0;
34                 }
35         }
36
37         static int GetTypes (Assembly a)
38         {
39                 try {
40                         Type[] ts = a.GetTypes ();
41                         Console.WriteLine ("*1* Can get all types from assembly '{0}' loaded. {1} types present.", filename, ts.Length);
42                         return 1;
43                 }
44                 catch (ReflectionTypeLoadException rtle) {
45                         Console.WriteLine ("*0* Expected ReflectionTypeLoadException\n{0}", rtle);
46                         Console.WriteLine ("Types ({0}):", rtle.Types.Length);
47                         for (int i=0; i < rtle.Types.Length; i++) {
48                                 Console.WriteLine ("\t{0}", rtle.Types [i]);
49                         }
50                         Console.WriteLine ("LoaderExceptions ({0}):", rtle.LoaderExceptions.Length);
51                         for (int i=0; i < rtle.LoaderExceptions.Length; i++) {
52                                 Console.WriteLine ("\t{0}", rtle.LoaderExceptions [i]);
53                         }
54                         return 0;
55                 }
56         }
57
58         static int Main ()
59         {
60                 try {
61                         Assembly a = Assembly.Load (filename);
62                         if (a == null) {
63                                 Console.WriteLine ("*2* Couldn't load assembly '{0}'.", filename);
64                                 return 2;
65                         }
66
67                         string typename = "NoSecurity";
68                         Type t = a.GetType (typename);
69                         if (t == null) {
70                                 Console.WriteLine ("*3* Cannot get type '{0}' without security.", typename);
71                                 return 3;
72                         }
73
74                         int err = GetTypeFalse (a);
75                         if (err != 0)
76                                 return err;
77
78                         err = GetTypeTrue (a);
79                         if (err != 0)
80                                 return err;
81
82                         err = GetTypes (a);
83                         return err;
84                 }
85                 catch (ReflectionTypeLoadException rtle) {
86                         Console.WriteLine ("*4* Expected ReflectionTypeLoadException\n{0}", rtle);
87                         return 4;
88                 }
89                 catch (SecurityException se) {
90                         Console.WriteLine ("*5* Unexpected SecurityException\n{0}", se);
91                         return 5;
92                 }
93                 catch (Exception e) {
94                         Console.WriteLine ("*6* Unexpected Exception\n{0}", e);
95                         return 6;
96                 }
97         }
98 }