可一个String []里面抱System.Object的? [英] Can a String[] hold System.Object inside it?

查看:201
本文介绍了可一个String []里面抱System.Object的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你觉得问题是奇怪吗?是什么事情也怪。让我来解释一下。

Do you feel question is strange? yes what happened also strange. let me explain.

我发现从这个片段<一个href=\"http://stackoverflow.com/questions/17619139/covariance-and-contravariance-with-c-sharp-arrays\">Covariance和逆变用C#阵列

string[] strings = new string[1];
object[] objects = strings;
objects[0] = new object();

乔恩斯基特解释说,上述code会抛出 ArrayTypeMismatchException ,因为说好它。

我是我把一个断点在第3行,使用DebuggerVisualizer手动设置对象[0]​​ =新的对象()它不抛出任何错误,有用。后来检查串[0] .GetType()返回System.Object的。不仅 System.Object的任何类型的可以在字符串中设置[]由上述步骤。

what I did is I put a breakpoint in line 3, Using DebuggerVisualizer I manually set objects[0] = new object() it doesn't throw any error and it works. later checking strings[0].GetType() returns System.Object. not only System.Object any type can be set in string[] by above mentioned procedure.

我不知道这是怎么发生我在同样的问题,我看到了这一点,但没有答案,提出我的问题作为一个评论在那边。

I have no idea how this happened i raised my question as a comment over there in the very same question i saw this but no answers.

很想知道发生了什么后面。任何人解释PLS。

Am curious to know what is happening behind. Anybody explain pls.

再现上述​​行为尝试之后

After reproducing the above behaviour try this

int len = strings[0].Length;

如果你把鼠标放在属性长度是说串[0]。长度扔的ArgumentException 与消息上找不到对象实例的方法,但实际上它不抛出异常和code运行产生结果 LEN = 0

if you place mouse over the Property Length is says strings[0].Length threw ArgumentException with message Cannot find the method on the object instance but actually it doesnt throw exception and code runs yielding result len=0

推荐答案

您例子似乎回答这样的问题:是的,字符串引用可非字符串对象。这不但是意。

Your example seems to answer the question: yes, a string reference can refer a non-string object. This is not intended, however.

在调试器想想你有什么发现,错误

Consider what you have found, a bug in the debugger.

由于乔恩斯基特解释你提到中的答案,因为.NET数组有这样的疯狂covaraiance即使阵列是不是只读的,但更像是可读写的,每次有一个 的写入引用框架必须二选一尝试写入到阵列的对象类型的数组,并抛出一个 ArrayTypeMismatchException 如果你要使用一个错误的类型,喜欢<$ C $的数组赋值的一个实例C>狗 S(运行时狗[] )已投用疯狂协成动物[ ]

As Jon Skeet explains in the answer you mention, because .NET arrays have this "crazy" covaraiance even though arrays are not read-only but more like read-write, everytime one writes to an array of references the framework has to check the type of the object one tries to write to the array, and throw an ArrayTypeMismatchException if you're about to use a wrong type, like assigning an instance of Cat to an array of Dogs (a runtime Dog[]) which has been cast by "crazy" covariance into an Animal[].

今天我向您展示的是,当我们使用Visual Studio调试器(或类似的窗口)的立即窗口,这需要类型检查不这样做,作为一个结果,这可能会导致任何类型的(除了可能的指针类型)被分配给任何的引用类型 X 的引用类型变量。像这样的:

What you have demonstrated is that when we use the Immediate window of the Visual Studio debugger (or similar windows), this required type check is not done, and as a result this can lead to any type Y (except pointer types probably) being assigned to a reference type variable of any reference type X. Like this:

X[] arrayOfX = new X[1];
object[] arrayCastByCrazyCovariance = arrayOfX;
Y badObject = new Y();  // or another constructor or method to get a Y

// Set breakpoint here.
// In Immediate window assign:  arrayCastByCrazyCovariance[0] = badObject
// Detach debugger again.

X anomalousReferenceVariable = arrayOfX[0];

anomalousReferenceVariable.MemberOfX();  // or other bad things

这可以使树皮像,和类似的东西。

This can make a Cat bark like a Dog, and stuff like that.

在上绕过型保障链接线,由codesInChaos的回答显示了一个不相关的技术,使用它可以把一个参考了错误和无关类型的对象为参考变量。

In the linked thread on Bypassing type safeguards, the answer by CodesInChaos shows an unrelated technique with which you can put a reference to an object of a "wrong" and unrelated type into a reference variable.

这篇关于可一个String []里面抱System.Object的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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