C#4.0引入了一种名为"dynamic"的新类型.这听起来不错,但程序员会用它做什么?
有没有可以节省一天的情况?
我认为我的大脑中途包含了C#4中的动态类型概念,但在我的生活中无法找到我真正想要使用它的场景.
我确信有很多,但我只是无法建立连接,以便我能够设计出一个更好的解决方案,而不是接口,依赖注入等动态解决方案.
那么,动态类型使用是否恰当的实际应用场景是什么?
我有这个非常简单的方法如下:
//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)