C#4.0引入了一种名为"dynamic"的新类型.这听起来不错,但程序员会用它做什么?
有没有可以节省一天的情况?
如果在调用方法时它们可能不存在,它们的用途是什么?
这是否意味着您可以在动态对象上动态创建方法?
这有什么实际用处?
我希望看到使用的一个领域dynamic是XML.我认为它会使XML处理代码更容易编写,我相信在C#4出现之前我已经看到了一些例子,并且在这个答案中提到它是此功能的一个用途.
所以,我的问题是:为什么不是XmlDocument(或XDocument)动态,或者为什么在C#4中没有动态XML操作的新类?
这对我来说更加奇怪,当我认为在PowerShell中,它XmlDocument 是动态的,代码就像$xmlDoc.root.subnode.subsubnode在那里工作一样.
我有这个非常简单的方法如下:
//my current file
using Newtonsoft.Json;
string key1 = "FirstKey";
string key2 = "SecondKey";
string key3 = "ThirdKey";
private string CreateJson(string val1, string val2, string val3,string val4, string val5, string val6)
{
//process the six arguments and three key-related member variables to create a JSON array
//don't want to use JsonConvert.SerializeObject the way I'm doing below as it requires creating a class
var configs = new List<CustomClass>
{ new CustomClass{ FirstKey = val1,SecondKey= val2,ThirdKey= val3}
, new CustomClass{ FirstKey= val4,SecondKey= …Run Code Online (Sandbox Code Playgroud)