本文介绍了使用“ var”和“ dynamic”时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 var 关键字第一次对我造成了一些异常。

I've run into a bit on an Anomaly where for the first time ever, using the var keyword bit me.

采用这种非常简单的方法

Take this very simple method

public static Int32? GetNullableInt32(Int32 num)
{
    return new Nullable<Int32>(num);
}

现在,我们可以使用 dynamic 参数,一切都会按预期进行。

Now we can call this method with a dynamic parameter and everything will work as expected.

public static void WorksAsAdvertised()
{
    dynamic thisIsAnInt32 = 42;

    //Explicitly defined type (no problems)
    Int32? shouldBeNullableInt32 = GetNullableInt32(thisIsAnInt32);

    Console.Write(shouldBeNullableInt32.HasValue);
}

但是,通过声明 shouldBeNullableInt32 使用隐式类型输入,结果与 I 预期的结果相去甚远。

However, by declaring shouldBeNullableInt32 using implicit typing, the results are far from what I would expect.

public static void BlowsUpAtRuntime()
{
    dynamic thisIsAnInt32 = 42;

    //Now I'm a dynamic{int}... WTF!!!
    var shouldBeNullableInt32 = GetNullableInt32(thisIsAnInt32);

    //Throws a RuntimeBinderException
    Console.Write(shouldBeNullableInt32.HasValue);
}

而不是成为 Nullable< Int32> 返回值get被视为动态类型。即使这样,底层的 Nullable< T> 也不会保留。由于 System.Int32 没有名为 HasValue 的属性,因此 RuntimeBinderException

Instead of being a Nullable<Int32> the return value get's treated as a dynamic type. And even then, the underlying Nullable<T> is not preserved. Since System.Int32 has no property named HasValue, a RuntimeBinderException is thrown.

我很想听到能真正解释正在发生的事情(而不仅仅是猜测)的人的消息。 p>

两个问题



I would be VERY curious to hear from someone who can actually explain what is happening (not just guess).


  1. 为什么 shouldBeNullableInt32 GetNullableInt32 的返回类型明显返回 Nullable< Int32> 时,$ c>获取隐式键入为动态 code>?

  2. 为什么不保留基础 Nullable< Int32> ?为什么要使用 dynamic {int} 已在此处回答:)

  1. Why does shouldBeNullableInt32 get implicitly typed as a dynamic when the return type of GetNullableInt32 clearly returns a Nullable<Int32>?
  2. Why is the underlying Nullable<Int32> not preserved? Why a dynamic{int} instead? (Answered here: C# 4: Dynamic and Nullable<>)






UPDATE



和同样有效。请同时阅读它们:)


UPDATE

Both Rick Sladkey's answer and Eric Lippert's answer are equally valid. Please read them both :)

推荐答案


  1. 为什么<$ c当 GetNullableInt32 的返回类型明显返回<$ c $时,$ c> shouldBeNullableInt32 得到隐式键入为动态 c> Nullable< Int32>

  1. Why does shouldBeNullableInt32 get implicitly typed as a dynamic when the return type of GetNullableInt32 clearly returns a Nullable<Int32>?


对我们来说显而易见的是,由于,执行被调用的实际方法被推迟到运行时,因为它被调用了具有动态参数。 GetNullableInt32 可能还有另一个重载,它与 thisIsAnInt32 的运行时值更好地匹配。直到运行时才知道的另一种方法,可能返回除 Int32之外的其他类型?

This is because while it is apparent to us that GetNullableInt32 is the method that is going to be called, because of dynamic binding, the actual method that does get called is deferred until run-time because it is being called with a dynamic parameter. There might be another overload of GetNullableInt32 that matches better the run-time value of thisIsAnInt32. That alternate method, which cannot be known until run-time, might return some other type than Int32?!

结果,由于动态绑定而不是静态绑定,编译器无法假定表达式的返回类型在编译时是什么,因此表达式返回类型动态。可以将鼠标悬停在 var 上。

As a result, the compiler, due to dynamic binding instead of static binding, cannot assume what the return type of the expression is at compile time and so the expression returns type dynamic. This can be seen by hovering over var.

您似乎已经对第二个问题做出了令人满意的解释问题在这里:

You appear to have already come to a satisfactory explanation for your second question here:



  • C# 4: Dynamic and Nullable<>

这篇关于使用“ var”和“ dynamic”时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:33