Merge pull request #5082 from kumpera/fix-ro-fs-file-delete
[mono.git] / mono / tests / bug-348522.2.cs
1 //
2 // From test: Bug 348522
3 //
4 using System;
5 using System.Reflection;
6 using System.Globalization;
7
8 public struct SimpleStruct {
9         public int a;
10         public int b;
11
12         public SimpleStruct (int a, int b)
13         {
14                 this.a = a;
15                 this.b = b;
16         }
17 }
18
19 class NullableTestClass
20 {
21         public bool hasValue;
22         public int bVal;
23
24         public void F (SimpleStruct? code)
25         {
26                 if (hasValue = code.HasValue)
27                         bVal = code.Value.b;
28         }
29 }
30
31 class PrimitiveTestClass
32 {
33         public int val;
34
35         public void i4 (int code) {
36                 val = code;
37         }
38 }
39
40 struct GenericStruct<T>
41 {
42         T t;
43 }
44
45 class GenericClass<T>
46 {
47         T t;
48 }
49
50 class Driver
51 {
52         public static GenericStruct<T> StructTest <T> (GenericStruct <T> t)
53         {
54                 return t;
55         }
56
57         public static GenericClass<T> ReferenceTest <T> (GenericClass <T> t)
58         {
59                 return t;
60         }
61
62         static int Main ()
63         {
64                 BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
65                 MethodInfo mi = typeof (NullableTestClass).GetMethod ("F");
66                 NullableTestClass nullable = new NullableTestClass ();
67                 SimpleStruct? test = new SimpleStruct (90, 90);
68
69
70                 mi.Invoke (nullable, flags, new PassesStuffBinder (null), new object [] {null}, null);
71                 if (nullable.hasValue) {
72                         Console.WriteLine ("invoked nullabled with null arg but did not get a null in the method");
73                         return 1;
74                 }
75
76
77                 nullable = new NullableTestClass ();
78                 mi.Invoke (nullable, flags, new PassesStuffBinder (new SimpleStruct (10, 20)), new object [] {200}, null);
79                 if (!nullable.hasValue || nullable.bVal != 20) {
80                         Console.WriteLine ("invoked nullabled with boxed struct, but did not get it");
81                         return 2;
82                 }
83                 
84
85                 nullable = new NullableTestClass ();
86                 mi.Invoke (nullable, flags, new PassesStuffBinder (test), new object [] {200}, null);
87                 if (!nullable.hasValue || nullable.bVal != 90) {
88                         Console.WriteLine ("invoked nullabled with nullable literal, but did not get it");
89                         return 3;
90                 }
91
92                 mi = typeof (PrimitiveTestClass).GetMethod ("i4");
93                 PrimitiveTestClass prim = new PrimitiveTestClass ();
94                 mi.Invoke (prim, flags, new PassesStuffBinder ((byte)10), new object [] {88}, null);
95                 if (prim.val != 10) {
96                         Console.WriteLine ("invoked primitive with byte, it should be widened to int "+ prim.val);
97                         return 4;
98                 }
99
100                 try {
101                         mi.Invoke (prim, flags, new PassesStuffBinder (Missing.Value), new object [] {null}, null);
102                         Console.WriteLine ("invoked literal with reference value");
103                         return 5;
104                 } catch (Exception) {
105
106                 }               
107
108                 try {
109                         MethodInfo method = typeof (Driver).GetMethod ("StructTest");
110                         MethodInfo generic_method = method.MakeGenericMethod (typeof (int));
111                         generic_method.Invoke (null, new object [] { new GenericStruct<int>() });
112
113                         method = typeof (Driver).GetMethod ("ReferenceTest");
114                         generic_method = method.MakeGenericMethod (typeof (int));
115                         generic_method.Invoke (null, new object [] { new GenericClass<int>() });
116                 } catch (Exception e) {
117                         Console.WriteLine ("calling with generic arg failed "+e);
118                         return 6;
119                 }
120
121                 return 0;
122         }
123 }
124
125 class PassesStuffBinder : BaseBinder
126 {
127         object stuff = null;
128
129         public PassesStuffBinder (object stuff)
130         {
131                 this.stuff = stuff;
132         }
133
134         public override object ChangeType (object value, Type type1, CultureInfo culture)
135         {
136                 return stuff;
137         }
138 }
139
140
141 class BaseBinder : Binder {
142         public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase [] match, ref object [] args,
143                                                  ParameterModifier [] modifiers, CultureInfo culture, string [] names,
144                                                  out object state)
145         {
146                 state = null;
147                 return match [0];
148         }
149         
150         public override object ChangeType (object value, Type type1, CultureInfo culture)
151         {
152                 return (ulong) 0xdeadbeefcafebabe;
153         }
154         
155         // The rest is just to please the compiler
156         public override FieldInfo BindToField (System.Reflection.BindingFlags a,
157                                                System.Reflection.FieldInfo[] b, object c, System.Globalization.CultureInfo d)
158         {
159                 return null;
160         }
161         
162         public override void ReorderArgumentArray(ref object[] a, object b) {
163         }
164         
165         public override MethodBase SelectMethod(System.Reflection.BindingFlags
166                                                 a, System.Reflection.MethodBase[] b, System.Type[] c,
167                                                 System.Reflection.ParameterModifier[] d) {
168                 return null;
169         }
170         
171         public override PropertyInfo 
172             SelectProperty(System.Reflection.BindingFlags a,
173                            System.Reflection.PropertyInfo[] b, System.Type c, System.Type[] d,
174                            System.Reflection.ParameterModifier[] e) {
175                 return null;
176         }
177 }