- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
这里是讨论的示例代码(考虑 Reptile"is"Animal 和 Mammal“也是”Animal)
Animal[] reptiles = new Reptile[]
{ new Reptile("lizard"), new Reptile("snake") };
Animal[] animals = new Animal[]
{ new Reptile("alligator"), new Mammal("dolphin") };
try
{
Array.ConstrainedCopy(animals, 0, reptiles, 0, 2);
}
catch (ArrayTypeMismatchException atme)
{
Console.WriteLine('[' + String.Join<Animal>(", ", reptiles) + ']');
}
当我运行这段代码时,我得到一个 ArrayTypeMismatchException,作为注释
Array.ConstrainedCopy will only work on array types that are provably compatible, without any form of boxing, unboxing, widening, or casting of each array element. Change the array types (i.e., copy a Derived[] to a Base[]), or use a mitigation strategy in the CER for Array.Copy's less powerful reliability contract, such as cloning the array or throwing away the potentially corrupt destination array.
但是,当我查看 MSDN 时我看到此方法还会抛出 InvalidCastException
。抛出 InvalidCastException
的条件是:
At least one element in sourceArray cannot be cast to the type of destinationArray.
所以我很困惑,如果正如它所说永远不能对数组元素进行任何转换,您如何从该方法中得到 InvalidCastException?
最佳答案
如果无法访问 Array.Copy
的实际 native 实现,我们最多只能检查 Shared Source CLI .以下是 clr\src\vm\comsystem.cpp 中的相关代码行:
FCIMPL6(void, SystemNative::ArrayCopy, ArrayBase* m_pSrc, INT32 m_iSrcIndex, ArrayBase* m_pDst, INT32 m_iDstIndex, INT32 m_iLength, CLR_BOOL reliable)
{
// ...
r = CanAssignArrayTypeNoGC(gc.pSrc, gc.pDst);
if (r == AssignWrongType) {
// [Throw ArrayTypeMismatchException]
}
if (r == AssignWillWork) {
// [Copy the array using memmove, which won't throw any exception]
return;
}
else if (reliable) {
// [Throw ArrayTypeMismatchException]
}
// [Handle other cases]
}
当 Array.ConstrainedCopy
调用 SystemNative::ArrayCopy
并将 reliable
参数设置为 TRUE
时,要么使用 memmove
复制数组或抛出 ArrayTypeMismatchException
。在这两种情况下都不会抛出 InvalidCastException
。
关于c# - 如何从 Array.ConstrainedCopy 获取 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18865207/
我是一名优秀的程序员,十分优秀!