为什么是铸造类对象的动态对象抛出空引用异常? [英] Why is casting a dynamic of type object to object throwing a null reference exception?
问题描述
我有以下功能:
公共静态牛逼TryGetArrayValue< T>(对象[] array_,INT INDEX_)
{
... //一些检查涨这里不相关质疑 动态盒装= array_ [INDEX_]
回报(T)盒装;
}
当我把它以下列方式,
对象=新的对象();
对象V = TUtils.TryGetArrayValue&所述;对象>(新的对象[] {A},0);
(T)盒装
抛出一个空引用异常。
任何其他类型的我把比对象,还有其他的,它工作完全正常。结果
任何想法这是什么,以及为什么它抛出异常?
编辑:
为什么我用动态的原因是为了避免异常类型转换的时候,例如:
双A = 123;
INT V = TUtils.TryGetArrayValue&所述; INT>(新的对象[] {A},0);
我同意与其他回答者谁说,这看起来像一个错误。特别是,它似乎是在C#运行时绑定层中的错误,虽然我还没有彻底调查它。
我的错误道歉。我会把它报告给C#5测试团队,我们将看看它是否已经被报告并固定在C#5(它再现了最近的测试版,所以这是不可能的,它已经被报道和固定。 )如果不是这样,一个解决方法是不可能把它做成了最终版本。在这种情况下,我们会考虑的一个可能的服务版本。
感谢您将这一引起我们的注意。如果你觉得进入了一个连接问题跟踪它,随意这样做,请包括一个链接到这个计算器的问题。如果你不这样做,没有问题;测试团队将知道它无论哪种方式。
I have the following function:
public static T TryGetArrayValue<T>(object[] array_, int index_)
{
... //some checking goes up here not relevant to question
dynamic boxed = array_[index_];
return (T)boxed;
}
When I call it in the following way,
object a = new object();
object v = TUtils.TryGetArrayValue<object>(new object[] { a }, 0);
(T)boxed
throws a null reference exception.
Any other type I put in there other than "object", it works perfectly fine.
Any ideas what this is, and why it's throwing the exception?
Edit: The reason why I use dynamic is to avoid exception when converting types, for example:
double a = 123;
int v = TUtils.TryGetArrayValue<int>(new object[] { a }, 0);
I agree with the other answerers who say that this looks like a bug. Specifically it appears to be a bug in C# runtime binding layer, though I have not investigated it thoroughly.
I apologize for the error. I'll report it to the C# 5 testing team and we'll see if it has already been reported and fixed in C# 5. (It reproduces in the recent beta release, so it is unlikely that it has already been reported and fixed.) If not, a fix is unlikely to make it into the final release. In that case we'll consider it for a possible servicing release.
Thanks for bringing this to our attention. If you feel like entering a Connect issue to track it, feel free to do so and please include a link to this StackOverflow question. If you don't, no problem; the test team will know about it either way.
这篇关于为什么是铸造类对象的动态对象抛出空引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!