try {
destinationArray.SetValueImpl (srcval, dest_pos + i);
+ } catch (ArgumentException) {
+ throw CreateArrayTypeMismatchException ();
} catch {
- if (src_type.Equals (typeof (Object)))
- throw new InvalidCastException ();
- else
- throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
- "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
+ if (CanAssignArrayElement (src_type, dst_type))
+ throw;
+
+ throw CreateArrayTypeMismatchException ();
}
}
}
try {
destinationArray.SetValueImpl (srcval, dest_pos + i);
+ } catch (ArgumentException) {
+ throw CreateArrayTypeMismatchException ();
} catch {
- if (src_type.Equals (typeof (Object)))
- throw new InvalidCastException ();
- else
- throw new ArrayTypeMismatchException (String.Format (Locale.GetText (
- "(Types: source={0}; target={1})"), src_type.FullName, dst_type.FullName));
+ if (CanAssignArrayElement (src_type, dst_type))
+ throw;
+
+ throw CreateArrayTypeMismatchException ();
}
}
}
}
+ static Exception CreateArrayTypeMismatchException ()
+ {
+ return new ArrayTypeMismatchException ();
+ }
+
+ static bool CanAssignArrayElement (Type source, Type target)
+ {
+ if (source.IsValueType)
+ return source.IsAssignableFrom (target);
+
+ if (source.IsInterface)
+ return !target.IsValueType;
+
+ if (target.IsInterface)
+ return !source.IsValueType;
+
+ return source.IsAssignableFrom (target) || target.IsAssignableFrom (source);
+ }
+
[ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray,
long destinationIndex, long length)
{
char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
- public ArrayTest() {}
+ interface I
+ {
+ }
+
+ class C
+ {
+ }
+
+ class DC : C
+ {
+ }
+
+ class DI : I
+ {
+ }
[Test]
public void TestIsFixedSize() {
}
[Test]
- [ExpectedException (typeof (InvalidCastException))]
public void Copy_InvalidCast () {
object[] arr1 = new object [10];
Type[] arr2 = new Type [10];
-
arr1 [0] = new object ();
- Array.Copy (arr1, 0, arr2, 0, 10);
+ try {
+ Array.Copy (arr1, 0, arr2, 0, 10);
+ Assert.Fail ("#1");
+ } catch (InvalidCastException) {
+ }
+
+ var arr1_2 = new I [1] { new DI () };
+ var arr2_2 = new C [1] { new DC () };
+ try {
+ Array.Copy (arr2_2, arr1_2, 1);
+ Assert.Fail ("#1");
+ } catch (InvalidCastException) {
+ }
}
[Test]