Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bug-348522.2.cs « tests « mono - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d845024b284aa01b0d68776367a030c5db94c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// From test: Bug 348522
//
using System;
using System.Reflection;
using System.Globalization;

public struct SimpleStruct {
	public int a;
	public int b;

	public SimpleStruct (int a, int b)
	{
		this.a = a;
		this.b = b;
	}
}

class NullableTestClass
{
	public bool hasValue;
	public int bVal;

	public void F (SimpleStruct? code)
	{
		if (hasValue = code.HasValue)
			bVal = code.Value.b;
	}
}

class PrimitiveTestClass
{
	public int val;

	public void i4 (int code) {
		val = code;
	}
}

struct GenericStruct<T>
{
	T t;
}

class GenericClass<T>
{
	T t;
}

class Driver
{
	public static GenericStruct<T> StructTest <T> (GenericStruct <T> t)
	{
		return t;
	}

	public static GenericClass<T> ReferenceTest <T> (GenericClass <T> t)
	{
		return t;
	}

	static int Main ()
	{
		BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
		MethodInfo mi = typeof (NullableTestClass).GetMethod ("F");
		NullableTestClass nullable = new NullableTestClass ();
		SimpleStruct? test = new SimpleStruct (90, 90);


		mi.Invoke (nullable, flags, new PassesStuffBinder (null), new object [] {null}, null);
		if (nullable.hasValue) {
			Console.WriteLine ("invoked nullabled with null arg but did not get a null in the method");
			return 1;
		}


		nullable = new NullableTestClass ();
		mi.Invoke (nullable, flags, new PassesStuffBinder (new SimpleStruct (10, 20)), new object [] {200}, null);
		if (!nullable.hasValue || nullable.bVal != 20) {
			Console.WriteLine ("invoked nullabled with boxed struct, but did not get it");
			return 2;
		}
		

		nullable = new NullableTestClass ();
		mi.Invoke (nullable, flags, new PassesStuffBinder (test), new object [] {200}, null);
		if (!nullable.hasValue || nullable.bVal != 90) {
			Console.WriteLine ("invoked nullabled with nullable literal, but did not get it");
			return 3;
		}

		mi = typeof (PrimitiveTestClass).GetMethod ("i4");
		PrimitiveTestClass prim = new PrimitiveTestClass ();
		mi.Invoke (prim, flags, new PassesStuffBinder ((byte)10), new object [] {88}, null);
		if (prim.val != 10) {
			Console.WriteLine ("invoked primitive with byte, it should be widened to int "+ prim.val);
			return 4;
		}

		try {
			mi.Invoke (prim, flags, new PassesStuffBinder (Missing.Value), new object [] {null}, null);
			Console.WriteLine ("invoked literal with reference value");
			return 5;
		} catch (Exception) {

		}		

		try {
			MethodInfo method = typeof (Driver).GetMethod ("StructTest");
			MethodInfo generic_method = method.MakeGenericMethod (typeof (int));
			generic_method.Invoke (null, new object [] { new GenericStruct<int>() });

			method = typeof (Driver).GetMethod ("ReferenceTest");
			generic_method = method.MakeGenericMethod (typeof (int));
			generic_method.Invoke (null, new object [] { new GenericClass<int>() });
		} catch (Exception e) {
			Console.WriteLine ("calling with generic arg failed "+e);
			return 6;
		}

		return 0;
	}
}

class PassesStuffBinder : BaseBinder
{
	object stuff = null;

	public PassesStuffBinder (object stuff)
	{
		this.stuff = stuff;
	}

	public override object ChangeType (object value, Type type1, CultureInfo culture)
	{
		return stuff;
	}
}


class BaseBinder : Binder {
	public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase [] match, ref object [] args,
						 ParameterModifier [] modifiers, CultureInfo culture, string [] names,
						 out object state)
	{
		state = null;
		return match [0];
	}
	
	public override object ChangeType (object value, Type type1, CultureInfo culture)
	{
		return (ulong) 0xdeadbeefcafebabe;
	}
	
	// The rest is just to please the compiler
	public override FieldInfo BindToField (System.Reflection.BindingFlags a,
					       System.Reflection.FieldInfo[] b, object c, System.Globalization.CultureInfo d)
	{
		return null;
	}
	
	public override void ReorderArgumentArray(ref object[] a, object b) {
	}
	
	public override MethodBase SelectMethod(System.Reflection.BindingFlags
						a, System.Reflection.MethodBase[] b, System.Type[] c,
						System.Reflection.ParameterModifier[] d) {
		return null;
	}
	
	public override PropertyInfo 
	    SelectProperty(System.Reflection.BindingFlags a,
			   System.Reflection.PropertyInfo[] b, System.Type c, System.Type[] d,
			   System.Reflection.ParameterModifier[] e) {
		return null;
	}
}