dynamic关键字

public class TestClass
{
  public dynamic Values { get; set; }

}

public class Test

{

  static void Func(TestClass test)

  {

    if(test.Values != null)

    {

      // ...

    }

  }

}

反编译后的内容:

if ((<Func>o__SiteContainer0.<>p__Site1 == null)

{

  <Func>o__SiteContainer0.<>p__Site1 = CallSite<Func<CallSite, object, object, object>>.Create(

      Binder.BinaryOperation(CSharpBinderFlags.None,

      ExpressionType.NotEqual,

      typeof (Test),

      (IEnumerable<CSharpArgumentInfo>) new CSharpArgumentInfo[2]
      {
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, (string) null),
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant, (string) null)
      }));
}

总之,编译器会将Dynamic关键字修饰的字段在调用的位置生成一些Microsoft.CSharp.RuntimeBinder

命名空间中预定义的基础设施生成静态字段和方法,并通过为空判断实现缓存功能,提高运行效率的同时,利用反射和其他功能完成对动态字段的操作。

了解了两者之间的编译转换关系及该命名空间中的相关枚举类型的含义,可以很容易的分辨出.NET反编译代码中的那个object字段为dynamic字段,并将使用CallSite/SiteContainer/Site/Target/Binder等反编译代码调用恢复为正常的使用dynamic字段的代码。(Todo:列举具体事例)

原文地址:https://www.cnblogs.com/dadream/p/4110462.html