首页 > 编程语言 > c# dynamic的好处
2020
12-07

c# dynamic的好处

dynamic 可在反射、json反序列化时使用、已达到减少代码量的效果。看代码

using System;

namespace ConsoleApp2
{
  class Program
  {
    static void Main(string[] args)
    {
      System.Type t = typeof(Person);
      var obj = Activator.CreateInstance(t, null);
      t.InvokeMember("Talk", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { "hell world!" });

      dynamic obj2 = Activator.CreateInstance(t, null);
      obj2.Talk("hell world!");
    }
  }

  class Person
  {
    public void Talk(string msg)
    {
      Console.WriteLine(msg);
    }
  }
}
//发送请求
      string result = HttpUtil.HttpPost(uri, url, data, headerdata);

      Console.WriteLine("获取销售易表" + table + "数据:");
      Console.WriteLine(result);
      //数据反序列化为匿名对象
      var list = JsonConvert.DeserializeAnonymousType(result, new { records = new List<dynamic>() });

以上就是c# dynamic的好处的详细内容,更多关于c# dynamic的资料请关注自学编程网其它相关文章!

编程技巧