首页 > 编程开发 > C类语言    日期:2021-07-15 / 浏览

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的资料请关注其它相关文章!

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章