Javascript"var obj = new Object"等效于C#
问题描述:
是否可以像使用Javascript一样简单地在C#中创建和对象以及设置属性.
Is there an easy way to create and Object and set properties in C# like you can in Javascript.
JavaScript示例:
Example Javascript:
var obj = new Object;
obj.value = 123476;
obj.description = "this is my new object";
obj.mode = 1;
答
尝试使用c#匿名类
var obj = new {
value = 123475,
description = "this is my new object",
mode = 1 };
尽管有很多差异...
there are lots of differences though...
@Valera Kolupaev & @GlennFerrieLive 提到了另一种使用 dynamic 关键字的方法
@Valera Kolupaev & @GlennFerrieLive mentioned another approach with dynamic keyword