后面的System.Array之谜 [英] Mystery behind System.Array

查看:152
本文介绍了后面的System.Array之谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道的System.Array 是一个抽象类,无论数据类型[] 我们使用运行时产​​生了一些具体的实施我们以某种方式(虽然模糊)。

考虑下面的代码片段。

  INT [] someInts = {1,2,3,4};
IList的< INT>集合= someInts;
collection.Clear();
 

collection.Clear()抛出 NotSupportedException异常,没有什么令人惊讶的出现。当我检查了堆栈跟踪很惊讶地看到它显示了一些奇怪的类型 SZArrayHelper 在调用堆栈的顶部。

堆栈跟踪:

 在System.SZArrayHelper.Clear [T]()//注意这个..如何???
   在TestApplication.Program.Main()
 

为什么这是可能的吗?我就打电话清除()方法 INT [] 但怎么做呼叫转到 SZArrayHelper.Clear 。注意,清除是一个实例方法 SZArrayHelper 定义如下。

 私人无效清除< T>()
{
    抛出新NotSupportedException异常(Environment.GetResourceString(NotSupported_ReadOnlyCollection));
}
 

谁创造了SZArrayHelper的实例,并指出,清除方法的私人的。很迷茫,看看发生了什么。如果在SZArrayHelper的所有实例被创建和清除被称为那么辅助方法,这样调用应该在堆栈跟踪。但事实并非如此这里。

有人可以解释发生了什么幕后?

请注意:

  1. INT [] 是公正的例子,你可以pretty的很多模拟它与任何类型的数组。不仅清除方法添加包含等拥有同样的行为..

  2. 我试着用反射镜的插件,这给了相同的结果调试器来调试显示直接呼叫 SZArrayHelper.Clear< T>()

  3. 只是一个谷歌使我这个 .NET阵列,IList的,通用算法,什么对STL?。这有助于了解某种魔力会落后,但神秘依然还在。

解决方案

您还没有看到任何调用该方法,因为你自己调用它,奇怪的是这听起来。 SZArrayHelper 是一个数组围绕CLR包装,实现了的IList< T> 界面,有点像适配器模式。

从这个角度来说,它是有道理的 collection.Clear 调用 SZArrayHelper.Clear 直接

汉斯帕桑特解释得很清楚这里: http://stackoverflow.com/a/11164210/857807

We know System.Array is a abstract class and whatever DataType[] we use runtime creates some concrete implementation for us somehow (vague though).

Consider the following snippet.

int[] someInts = { 1, 2, 3, 4 };
IList<int> collection = someInts;
collection.Clear();

collection.Clear() throws NotSupportedException, Nothing surprising there. When I check to see the "StackTrace" am surprised to see it shows some strange "Type" SZArrayHelper at top of the call stack.

StackTrace:

   at System.SZArrayHelper.Clear[T]()//Note this.. How???
   at TestApplication.Program.Main()

How come that is possible? am calling Clear() method on int[] but then how does the call goes to SZArrayHelper.Clear. note that Clear is an instance method in SZArrayHelper defined as below.

private void Clear<T>()
{
    throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection"));
}

Who creates the instance of "SZArrayHelper" and also note that Clear method is private. Am very confused to see what's happening. If at all an instance of "SZArrayHelper" is created and Clear is called then that helper method doing this call should come in the "StackTrace". but that is not the case here.

Can somebody explain what's happening behind the scenes?

Note:

  1. int[] is just and example, you can pretty much simulate it with any type of array. and not only Clear method Add, Contains etc possesses same behavior..

  2. I tried to debug using reflector addin, which gave same results debugger shows direct call to SZArrayHelper.Clear<T>().

  3. just a google led me to this .NET Arrays, IList, Generic Algorithms, and what about STL?. That helped to understand some kind of magic is going behind but mystery remains still.

解决方案

You're not seeing any call to that method, because you're invoking it yourself, as weird as that may sound. SZArrayHelper is a CLR wrapper around an array, that implements the IList<T> interface, kinda like the adapter pattern.

From this perspective, it makes sense that collection.Clear invokes SZArrayHelper.Clear directly.

Hans Passant explains this very well here: http://stackoverflow.com/a/11164210/857807

这篇关于后面的System.Array之谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆