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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
//
// System.Reflection.BinderTests - Tests Type.DefaultBinder
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2004 Novell, Inc. (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
namespace MonoTests.System.Reflection
{
enum MyEnum {
Zero,
One,
Two
}
class SampleClass {
public static void SampleMethod (object o) { }
public Type this[decimal i] {
get { return i.GetType (); }
}
public Type this[object i] {
get { return i.GetType (); }
}
}
class SingleIndexer {
public Type this [int i] {
get { return i.GetType (); }
}
}
class MultiIndexer
{
public Type this[byte i] {
get { return i.GetType (); }
}
public Type this[sbyte i] {
get { return i.GetType (); }
}
public Type this[short i] {
get { return i.GetType (); }
}
public Type this[ushort i] {
get { return i.GetType (); }
}
public Type this[int i] {
get { return i.GetType (); }
}
public Type this[uint i] {
get { return i.GetType (); }
}
public Type this[long i] {
get { return i.GetType (); }
}
public Type this[ulong i] {
get { return i.GetType (); }
}
public Type this[float i] {
get { return i.GetType (); }
}
public Type this[double i] {
get { return i.GetType (); }
}
public Type this[decimal i] {
get { return i.GetType (); }
}
public Type this[object i] {
get { return i.GetType (); }
}
public Type this[Enum i] {
get { return i.GetType (); }
}
}
[TestFixture]
public class BinderTest
{
Binder binder = Type.DefaultBinder;
[Test]
[ExpectedException (typeof (ArgumentException))]
public void SelectPropertyTestNull1 ()
{
// The second argument is the one
binder.SelectProperty (0, null, null, null, null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void SelectPropertyTestEmpty ()
{
// The second argument is the one
binder.SelectProperty (0, new PropertyInfo [] {}, null, null, null);
}
[Test]
[ExpectedException (typeof (AmbiguousMatchException))]
public void AmbiguousProperty1 () // Bug 58381
{
Type type = typeof (MultiIndexer);
PropertyInfo pi = type.GetProperty ("Item");
}
[Test]
public void SelectAndInvokeAllProperties1 ()
{
Type type = typeof (MultiIndexer);
PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
// These don't cause an AmbiguousMatchException
Type [] types = { typeof (byte), typeof (short),
typeof (int), typeof (long),
typeof (MyEnum) };
/* MS matches short for sbyte!!! */
/* MS matches int for ushort!!! */
/* MS matches long for uint!!! */
/** These do weird things under MS if used together and then in separate arrays *
Type [] types = { typeof (ulong), typeof (float), typeof (double),
typeof (decimal), typeof (object) };
*/
MultiIndexer obj = new MultiIndexer ();
foreach (Type t in types) {
PropertyInfo prop = null;
try {
prop = binder.SelectProperty (0, props, null, new Type [] {t}, null);
} catch (Exception e) {
throw new Exception ("Type: " + t, e);
}
Type gotten = (Type) prop.GetValue (obj, new object [] {Activator.CreateInstance (t)});
Assert.AreEqual (t, gotten);
}
}
[Test]
public void SelectAndInvokeAllProperties2 ()
{
Type type = typeof (MultiIndexer);
PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
Type [] types = { typeof (ushort), typeof (char) };
MultiIndexer obj = new MultiIndexer ();
PropertyInfo prop1 = binder.SelectProperty (0, props, null, new Type [] {types [0]}, null);
PropertyInfo prop2 = binder.SelectProperty (0, props, null, new Type [] {types [1]}, null);
Assert.AreEqual (prop1, prop2);
}
[Test]
public void Select1Match2 ()
{
Type type = typeof (SingleIndexer);
PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [0], null);
Assert.IsNull (prop, "empty");
}
[Test]
public void Select1Match ()
{
Type type = typeof (SingleIndexer);
PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
PropertyInfo prop;
prop = binder.SelectProperty (0, props, null, new Type [] { typeof (long) }, null);
Assert.IsNull (prop, "long");
prop = binder.SelectProperty (0, props, null, new Type [] { typeof (int) }, null);
Assert.IsNotNull (prop, "int");
prop = binder.SelectProperty (0, props, null, new Type [] { typeof (short) }, null);
Assert.IsNotNull (prop, "short");
}
[Test]
public void ArgNullOnMethod () // see bug 58846. We throwed nullref here.
{
Type type = typeof (SampleClass);
BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod;
type.InvokeMember ("SampleMethod", flags, null, null, new object[] { null });
}
[Test]
public void ArgNullOnProperty ()
{
Type type = typeof (SampleClass);
PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [] {null}, null);
Assert.IsNotNull (prop);
}
[Test] // bug #41691
public void BindToMethodNamedArgs ()
{
Type t = typeof (Bug41691);
StringWriter sw = new StringWriter ();
sw.NewLine = "\n";
object[] argValues = new object [] {"Hello", "World", "Extra", sw};
string [] argNames = new string [] {"firstName", "lastName"};
t.InvokeMember ("PrintName",
BindingFlags.InvokeMethod,
null,
null,
argValues,
null,
null,
argNames);
Assert.AreEqual ("Hello\nExtra\nWorld\n", sw.ToString ());
}
public class Bug41691
{
public static void PrintName (string lastName, string firstName, string extra, TextWriter output)
{
output.WriteLine (firstName);
output.WriteLine (extra);
output.WriteLine (lastName);
}
}
[Test] // bug #42457
public void GetMethodAmbiguity ()
{
object IntegerObject = 5;
object IntArrayObject = new int[] {5, 2, 5};
object StringArrayObject = new string [] {"One", "Two"};
object [] IntParam = new object [] {IntegerObject};
object [] IntArrayParam = new object [] {IntArrayObject};
object [] StringArrayParam = new object [] {StringArrayObject};
object be = this;
Type betype = this.GetType ();
string name1 = "Bug42457Method";
string name2 = "Bug42457Method2";
MethodInfo mi_obj = betype.GetMethod (name1, Type.GetTypeArray (IntParam));
mi_obj.Invoke (be, IntParam);
Assert.AreEqual (1, bug42457, "#1");
MethodInfo mi_arr = betype.GetMethod (name1, Type.GetTypeArray (IntArrayParam));
mi_arr.Invoke (be, IntArrayParam);
Assert.AreEqual (2, bug42457, "#2");
MethodInfo mi_str = betype.GetMethod (name1, Type.GetTypeArray (StringArrayParam));
mi_str.Invoke (be, StringArrayParam);
Assert.AreEqual (3, bug42457, "#3");
MethodInfo m2_obj = betype.GetMethod (name2, Type.GetTypeArray (IntParam));
m2_obj.Invoke (be, IntParam);
Assert.AreEqual (1, bug42457_2, "#4");
MethodInfo m2_arr = betype.GetMethod (name2, Type.GetTypeArray (IntArrayParam));
m2_arr.Invoke (be, IntArrayParam);
Assert.AreEqual (2, bug42457_2, "#5");
MethodInfo m2_str = betype.GetMethod (name2, Type.GetTypeArray(StringArrayParam));
m2_str.Invoke (be, StringArrayParam);
Assert.AreEqual (3, bug42457_2, "#6");
}
static void MethodWithLongParam(long param)
{
}
[Test]
public void TestExactBinding ()
{
Type[] types = new Type[] { typeof(int) };
Assert.AreEqual (null, typeof (BinderTest).GetMethod("MethodWithLongParam", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.ExactBinding, null, types, null));
}
public void Bug42457Method (object thing)
{
bug42457 = 1;
}
public void Bug42457Method (Array thing)
{
bug42457 = 2;
}
public void Bug42457Method (string [] thing)
{
bug42457 = 3;
}
public void Bug42457Method2 (object thing)
{
bug42457_2 = 1;
}
public void Bug42457Method2 (Array thing)
{
bug42457_2 = 2;
}
public void Bug42457Method2 (string [] thing)
{
bug42457_2 = 3;
}
int bug42457, bug42457_2;
[Test] // bug #77079
public void GetMethodAvoidAmbiguity2 ()
{
Type tType = this.GetType ();
Bug77079A a = new Bug77079C ();
tType.InvokeMember ("Bug77079",
BindingFlags.Public | BindingFlags.InvokeMethod |
BindingFlags.Instance,
null, this, new object[] {a});
Assert.AreEqual (2, bug77079);
}
int bug77079;
public void Bug77079 (Bug77079A a)
{
bug77079 = 1;
}
public void Bug77079 (Bug77079B a)
{
bug77079 = 2;
}
public class Bug77079A
{
}
public class Bug77079B : Bug77079A
{
}
public class Bug77079C : Bug77079B
{
}
}
}
|