2 // From test: Bug 348522
5 using System
.Reflection
;
6 using System
.Globalization
;
8 public struct SimpleStruct
{
12 public SimpleStruct (int a
, int b
)
19 class NullableTestClass
24 public void F (SimpleStruct
? code
)
26 if (hasValue
= code
.HasValue
)
31 class PrimitiveTestClass
35 public void i4 (int code
) {
40 struct GenericStruct
<T
>
52 public static GenericStruct
<T
> StructTest
<T
> (GenericStruct
<T
> t
)
57 public static GenericClass
<T
> ReferenceTest
<T
> (GenericClass
<T
> t
)
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");
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");
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");
92 mi
= typeof (PrimitiveTestClass
).GetMethod ("i4");
93 PrimitiveTestClass prim
= new PrimitiveTestClass ();
94 mi
.Invoke (prim
, flags
, new PassesStuffBinder ((byte)10), new object [] {88}
, null);
96 Console
.WriteLine ("invoked primitive with byte, it should be widened to int "+ prim
.val
);
101 mi
.Invoke (prim
, flags
, new PassesStuffBinder (Missing
.Value
), new object [] {null}
, null);
102 Console
.WriteLine ("invoked literal with reference value");
104 } catch (Exception
) {
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
);
125 class PassesStuffBinder
: BaseBinder
129 public PassesStuffBinder (object stuff
)
134 public override object ChangeType (object value, Type type1
, CultureInfo culture
)
141 class BaseBinder
: Binder
{
142 public override MethodBase
BindToMethod (BindingFlags bindingAttr
, MethodBase
[] match
, ref object [] args
,
143 ParameterModifier
[] modifiers
, CultureInfo culture
, string [] names
,
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
)
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
) {
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
) {