为什么我不能做到这一点:动态X =新ExpandoObject {美孚= 12,酒吧= QUOT十二个" } [英] Why can't I do this: dynamic x = new ExpandoObject { Foo = 12, Bar = "twelve" }

查看:103
本文介绍了为什么我不能做到这一点:动态X =新ExpandoObject {美孚= 12,酒吧= QUOT十二个" }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做得不对,或者是下面的code真的不可能?

 动态X =新ExpandoObject {美孚= 12,酒吧=十二条};

如果这真的是不可能的,有另一种单行的方式来实例化一个ExpandoObject带有两个属性?

为什么会C#团队齐齐禁止同一初始化语法为常规对象,匿名对象和可枚举/列表?

更新

我问这个问题,因为我是想表现出珍珠爱好者C#很酷的新动态功能,但后来我被不能够做什么,我认为是一个逻辑实例 ExpandoObject停滞。由于汉斯帕桑特的回答,我认识到, ExpandoObject 是错误的工作工具。我真正的目标是使用C#的动态特性,从方法返回两个命名值。正如汉斯指出,动态关键字是为这个完美的。我并不需要一个 ExpandoObject ,其所有的开销,做到这一点。

所以,如果你想从一个方法返回一个对命名值,而你不关心类型安全,智能感知,重构,还是性能,这个工程相当不错:

 公共动态CreateFooBar()
{
    返回新{美孚= 42,酒吧=你好};
}

用法:

 动态FOOBAR = CreateFooBar();
无功富= fooBar.Foo;
VAR栏= fooBar.Bar;


解决方案

  

我做得不对,或者是下面的code真的不可能?


这真是不可能的。赋值运算符左边的事情必须是在编译时已知的属性或领域,显然是不适合的expando对象的情况。


  

为什么会C#团队齐齐禁止同一初始化语法为常规对象,匿名对象和可枚举/列表?


这就是你那句问题表示逻辑错误。功能默认情况下不执行,然后我们跑来跑去不允许几乎所有的人,因为我们认为他们是一个坏主意!特点是的未实现的默认,而必须的实施的为了工作。

在实现任何功能的第一步是,有人在第一时间想起来。据我所知,我们从来没有。特别是,它本来是相当困难的人设计对象初始值设定在2006年要知道,在2010年,我们将要添加动态的语言,并据此设计的功能。功能总是被设计师设计谁动的转发的时间,不是的向后的时间。我们只记得过去,而不是未来。

不管怎么说,这是一个不错的主意十分感谢分享。现在已经有人想到这一点,我们就可以在接下来的步骤,就像决定,如果它是在我们可以用我们的预算有限,设计它,书写规范,实现它,测试它,记录它最好的主意和它向客户发货。

我不希望任何的,要很快发生;我们有点忙,我们在构建上周宣布这整个异步和WinRT的业务。

Am I doing something wrong, or is the following code really not possible?

dynamic x = new ExpandoObject { Foo = 12, Bar = "twelve" };

If this really isn't possible, is there another one-line way to instantiate an ExpandoObject with two properties?

Why would the C# team opt to disallow the same initialization syntax as for regular objects, anonymous objects, and enumerables/lists?

Update

I asked this question because I was trying show a Pearl enthusiast the cool new dynamic features of C#, but then I was stalled by not being able to do what I thought was a logical instantiation of an ExpandoObject. Thanks to Hans Passant's answer, I realize that ExpandoObject was the wrong tool for the job. My real goal was to use the dynamic features of C# to return two named values from a method. As Hans points out, the dynamic keyword is perfect for this. I didn't need an ExpandoObject, with all its overhead, to do this.

So, if you want to return a pair of named values from a method, and you are not concerned about type safety, Intellisense, refactoring, or performance, this works quite nicely:

public dynamic CreateFooBar()
{
    return new { Foo = 42, Bar = "Hello" };
}

Usage:

dynamic fooBar = CreateFooBar();
var foo = fooBar.Foo;
var bar = fooBar.Bar;

解决方案

Am I doing something wrong, or is the following code really not possible?

It's really not possible. The thing on the left of the assignment operator has to be a property or field known at compile time, and obviously that is not the case for expando objects.

Why would the C# team opt to disallow the same initialization syntax as for regular objects, anonymous objects, and enumerables/lists?

The way you phrase the question indicates the logical error. Features are not implemented by default and then we run around disallowing almost all of them because we think they're a bad idea! Features are unimplemented by default, and have to be implemented in order to work.

The first step in implementing any feature is that someone has to think of it in the first place. To my knowledge, we never did. In particular, it would have been quite difficult for the person designing object initializers in 2006 to know that in 2010 we were going to add "dynamic" to the language, and design the feature accordingly. Features are always designed by designers who move forwards in time, not backwards in time. We only remember the past, not the future.

Anyway, it's a nice idea so thanks for sharing it. Now that someone has thought of it, we can then work on the next steps, like deciding if it is the best idea upon which we can spend our limited budget, designing it, writing the specification, implementing it, testing it, documenting it and shipping it to customers.

I wouldn't expect any of that to happen any time soon; we're a bit busy with this whole async-and-WinRT business that we announced at Build last week.

这篇关于为什么我不能做到这一点:动态X =新ExpandoObject {美孚= 12,酒吧= QUOT十二个" }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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