Reduce TLS accesses. (#11487)
[mono-project.git] / mono / tests / bug-348522.2.cs
blob5d845024b284aa01b0d68776367a030c5db94c6e
1 //
2 // From test: Bug 348522
3 //
4 using System;
5 using System.Reflection;
6 using System.Globalization;
8 public struct SimpleStruct {
9 public int a;
10 public int b;
12 public SimpleStruct (int a, int b)
14 this.a = a;
15 this.b = b;
19 class NullableTestClass
21 public bool hasValue;
22 public int bVal;
24 public void F (SimpleStruct? code)
26 if (hasValue = code.HasValue)
27 bVal = code.Value.b;
31 class PrimitiveTestClass
33 public int val;
35 public void i4 (int code) {
36 val = code;
40 struct GenericStruct<T>
42 T t;
45 class GenericClass<T>
47 T t;
50 class Driver
52 public static GenericStruct<T> StructTest <T> (GenericStruct <T> t)
54 return t;
57 public static GenericClass<T> ReferenceTest <T> (GenericClass <T> t)
59 return t;
62 static int Main ()
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);
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;
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;
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;
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;
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) {
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>() });
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;
121 return 0;
125 class PassesStuffBinder : BaseBinder
127 object stuff = null;
129 public PassesStuffBinder (object stuff)
131 this.stuff = stuff;
134 public override object ChangeType (object value, Type type1, CultureInfo culture)
136 return stuff;
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)
146 state = null;
147 return match [0];
150 public override object ChangeType (object value, Type type1, CultureInfo culture)
152 return (ulong) 0xdeadbeefcafebabe;
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)
159 return null;
162 public override void ReorderArgumentArray(ref object[] a, object b) {
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;
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;