[英]Dynamic Property Assignment Throws RuntimeBinderException
I am getting a RuntimeBinderException
with the message 我收到一条带有消息的
RuntimeBinderException
Cannot implicitly convert type 'object' to 'MyNamespace.SomeEnum?'.
无法将类型'object'隐式转换为'MyNamespace.SomeEnum?'。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否缺少演员表?)
The following code generates that error: 以下代码生成该错误:
public enum SomeEnum
{
Val1
}
public class Example
{
public SomeEnum? EnumMember { get; set; }
}
public static class Program
{
static void Main()
{
dynamic example = new Example();
// Works without issue
example.EnumMember = (dynamic)Enum.Parse(typeof(SomeEnum), "Val1");
// Works without issue
example.EnumMember = Enum.Parse(example.EnumMember.GetType(), "Val1");
// Throws the aforementioned RuntimeBinderException
example.EnumMember = Enum.Parse(typeof(SomeEnum), "Val1");
}
}
Why do to first two lines work (both return type dynamic), but the third throws an exception (when the return type is object)? 为什么前两行有效(返回类型均为动态),但第三行抛出异常(当返回类型为object时)? I was under the impression that, when assigning to dynamic, the binding is performed using the actual, run-time type of the right-hand-side.
我的印象是,当分配给动态时,绑定是使用右侧的实际运行时类型执行的。 Can someone please enlighten me as to why the third line is unable to run as written?
有人可以启发我为什么第三行不能按原样运行吗?
The compile-time type of the expression on the RHS of the =
operator for the first two lines is dynamic
. 前两行
=
运算符在RHS上的表达式的编译时类型为dynamic
。 In the first case that's because you've cast to dynamic
, and in the second case it's because you're using a dynamic value in one of the arguments. 在第一种情况下,是因为您已强制转换为
dynamic
,在第二种情况下,是因为您在其中一个参数中使用了动态值。
In the third case, the compile-time type of the expression is object
. 在第三种情况下,表达式的编译时类型为
object
。 So you're trying to do the equivalent of: 因此,您正在尝试执行以下操作:
object x = Enum.Parse(typeof(SomeEnum), "Val1");
example.EnumMember = x;
That doesn't work, because there's no implicit conversion from object
to SomeEnum?
那是行不通的,因为没有从
object
到SomeEnum?
隐式转换SomeEnum?
, which is what the compiler is trying to find at execution time. ,这是编译器在执行时试图查找的内容。
Note that the nullability part really isn't relevant here - nor is the fact that it's an enum. 请注意,可空性部分在这里确实无关紧要-它也不是枚举。 It's just that the assignment operator is being bound dynamically, but using the compile-time time of the RHS.
只是赋值运算符是动态绑定的,而是使用RHS的编译时时间。 Here's a similar but simpler example:
这是一个类似但更简单的示例:
class Test
{
public int Foo { get; set; }
static void Main()
{
dynamic example = new Test();
example.Foo = 10; // Fine
object o = 10;
example.Foo = o; // Bang
}
}
If you want the compiler to handle the assignment dynamically using the actual type of the value returned rather than the compile-time type, then using dynamic
is exactly what you want to do - either cast to dynamic, or use: 如果希望编译器使用返回值的实际类型而不是编译时类型来
dynamic
处理分配,则使用dynamic
正是您想要做的-强制转换为dynamic或使用:
dynamic value = ...;
target.SomeProperty = value;
You still need to do an implicit conversion for the third line 您仍然需要对第三行进行隐式转换
example.EnumMember = (SomeEnum) Enum.Parse(typeof(SomeEnum), "Val1");
EDIT 编辑
The reason that you still need implicit conversion is because Enum.Parse
returns an object. 您仍然需要隐式转换的原因是因为
Enum.Parse
返回了一个对象。 Refer to the documentation below. 请参阅下面的文档。
https://msdn.microsoft.com/en-us/library/essfb559(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/essfb559(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.