为什么要将类型对象的动态转换为抛出空引用异常的对象?

问题描述:

我有以下功能:

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例外。

(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?

编辑:
我使用动态的原因是为了避免在转换类型时出现异常,例如:

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);


我同意其他回答者说这个样子像一个bug具体来说,它似乎是C#运行时绑定层中的一个错误,尽管我还没有彻底调查。

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.

对于错误,我深表歉意。我会将其报告给C#5测试团队,我们将看看它是否已经在C#5中被报告和修复。(它在最近的beta版本中复制,因此它不大可能已被报告和修复。 )如果没有,修复程序不太可能使其成为最终版本。在这种情况下,我们会考虑一下可能的服务版本。

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.

感谢您的关注。如果您觉得输入Connect问题来跟踪它,请随时这样做,并请包括一个链接到此StackOverflow题。如果没有,没问题;测试团队会以任何方式了解它。

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.