a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C# Dynamic關鍵字之:dynamic為什么比反射快的詳解

2020-01-24 03:20:27
字體:
來源:轉載
供稿:網友

Main方法如下:

復制代碼 代碼如下:

static void Main(string[] args)
{
    dynamic str = "abcd";
    Console.WriteLine(str.Length);

    Console.WriteLine();
    Console.WriteLine(str.Substring(1));


    Console.ReadLine();
}


運行,結果如下:

clip_image002

使用reflactor 反編譯下,可以看到:

完整代碼如下:

復制代碼 代碼如下:

private static void Main(string[] args)
{
      object obj1 = "abcd";
      if (Program.<Main>o__SiteContainer0.<>p__Site1 == null)
      {
            Program.<Main>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, Type, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "WriteLine", null, typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
      }
      if (Program.<Main>o__SiteContainer0.<>p__Site2 == null)
      {
            Program.<Main>o__SiteContainer0.<>p__Site2 = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "Length", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
      }
      Program.<Main>o__SiteContainer0.<>p__Site1.Target(Program.<Main>o__SiteContainer0.<>p__Site1, typeof(Console), Program.<Main>o__SiteContainer0.<>p__Site2.Target(Program.<Main>o__SiteContainer0.<>p__Site2, obj1));
      Console.WriteLine();
      if (Program.<Main>o__SiteContainer0.<>p__Site3 == null)
      {
            Program.<Main>o__SiteContainer0.<>p__Site3 = CallSite<Action<CallSite, Type, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "WriteLine", null, typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
      }
      if (Program.<Main>o__SiteContainer0.<>p__Site4 == null)
      {
            Program.<Main>o__SiteContainer0.<>p__Site4 = CallSite<Func<CallSite, object, int, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "Substring", null, typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant | CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
      }
      Program.<Main>o__SiteContainer0.<>p__Site3.Target(Program.<Main>o__SiteContainer0.<>p__Site3, typeof(Console), Program.<Main>o__SiteContainer0.<>p__Site4.Target(Program.<Main>o__SiteContainer0.<>p__Site4, obj1, 1));
      Console.ReadLine();
}

首先編譯器會自動生成一個靜態類:如下:
復制代碼 代碼如下:

[CompilerGenerated]
private static class <Main>o__SiteContainer0
{
      // Fields
      public static CallSite<Action<CallSite, Type, object>> <>p__Site1;
      public static CallSite<Func<CallSite, object, object>> <>p__Site2;
      public static CallSite<Action<CallSite, Type, object>> <>p__Site3;
      public static CallSite<Func<CallSite, object, int, object>> <>p__Site4;
}

為什么這里有四個CallSite<T>的對象呢?在我們的代碼中

Console.WriteLine(str.Length);
Console.WriteLine();
Console.WriteLine(str.Substring(1));
一共使用了四次dynamic對象。1:Console.WriteLine(dynamic); str.Length返回dynamic2:dynamic.Length;3:Console.WriteLine(dynamic); str.Substring 返回dynamic4:dynamic.Substring(1); 1,2,3,4,分別對應上面的<>p_Site1,2,3,4;

因為1,3 都是無返回值的,所以是Action, 2,4都有返回值,所以是Func. 看上面的代碼可能還不清楚,讓我們手動的生成下代碼吧:新建類SiteContainer 來取代編譯器自動生成的類。

復制代碼 代碼如下:

[CompilerGenerated]
public static class SiteContainer
{
  // Fields
  public static CallSite<Action<CallSite, Type, object>> p__Site1;
  public static CallSite<Func<CallSite, object, object>> p__Site2;
  public static CallSite<Action<CallSite, Type, object>> p__Site3;
  public static CallSite<Func<CallSite, object, int, object>> p__Site4;
}

接著看下初始化p__Site1時的方法吧:
復制代碼 代碼如下:

if (SiteContainer.p__Site1 == null)
{
    CallSiteBinder csb= Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(
        CSharpBinderFlags.ResultDiscarded,
        "WriteLine", null, typeof(Program),
        new CSharpArgumentInfo[]
        {
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType,null),
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None,null)
        });
    SiteContainer.p__Site1 = CallSite<Action<CallSite, Type, object>>.Create(csb);
}

InvokeMember方法的簽名:public static CallSiteBinder InvokeMember(CSharpBinderFlags flags, string name, IEnumerable<Type> typeArguments, Type context, IEnumerable<CSharpArgumentInfo> argumentInfo); 1:在這里CSharpBinderFlags傳遞的是ResultDiscarded,代表結果被丟棄,   所以可以綁定到一個void的返回方法中。2:name傳遞的是”WriteLine”,要調用的方法的名稱。3:typeArguments.類型參數的列表,傳遞null。4:context: 用于指示此操作的使用位置的 System.Type,在這里是Program5:argumentInfo:參數信息,   接著看看p__Site2如何初始化的吧:
復制代碼 代碼如下:

if (SiteContainer.p__Site2 == null)
{
    CallSiteBinder csb = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
        CSharpBinderFlags.None, "Length", typeof(Program),
        new CSharpArgumentInfo[]
        {
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
        });

    SiteContainer.p__Site2 = CallSite<Func<CallSite, object, object>>.Create(csb);
}

可以看到,和p__Site1不同的是,調用的是GetMember方法。
 
既然有了兩個CallSite<T>的對象,那么它們又是如何調用的呢??
使用CallSite<T>.Target 就可以調用了。
 
image 
 
//這是編譯器生成的代碼://SiteContainer.p__Site1.Target(SiteContainer.p__Site1, typeof(Console), // SiteContainer.p__Site2.Target(SiteContainer.p__Site2, obj1) //); var pSite2Result = SiteContainer.p__Site2.Target(SiteContainer.p__Site2, obj1); SiteContainer.p__Site1.Target(SiteContainer.p__Site1, typeof(Console), pSite2Result);

 
看看如何調用的吧:
因為SiteContainer.p__Site2,是調用Length屬性
首先調用p__Site2的target方法,執行p__Site2,對象是obj1.
dlr 就會調用obj1.Length,并返回結果,所以pSite2Result=4;
接著調用p__Site1的target,來調用Console類的WriteLine方法,參數是pSite2Result.所以輸出4.
 
最后來看下dynamic是如何調用Substring方法的:

Substring方法對應的是p__Site4,因為Substring方法傳遞了個參數1,并且有返回值,所以

p__Site4對象是:

public static CallSite<Func<CallSite, object, int, object>> p__Site4;
初始化:

復制代碼 代碼如下:

if (SiteContainer.p__Site4 == null)
{
    CallSiteBinder csb = Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(
        CSharpBinderFlags.None, "Substring", null, typeof(Program),
        new CSharpArgumentInfo[]
        {
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant
            | CSharpArgumentInfoFlags.UseCompileTimeType, null)                       
        });
    SiteContainer.p__Site4 = CallSite<Func<CallSite, object, int, object>>.Create(csb);
}

基本和上面的p__Site1類似,只是參數信息是:CSharpArgumentInfoFlags.Constant /

因為調用了Substring(1).在編譯的時候會傳遞1進去,而1是常量。 調用如下:

復制代碼 代碼如下:

var subStringResult = SiteContainer.p__Site4.Target(SiteContainer.p__Site4, obj1, 1);
SiteContainer.p__Site1.Target(SiteContainer.p__Site1, typeof(Console), subStringResult);

解釋同上。

完整的Main函數代碼如下:

復制代碼 代碼如下:

static void Main(string[] args)
{
    object obj1 = "abcd";

    if (SiteContainer.p__Site1 == null)
    {
        CallSiteBinder csb = Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(
            CSharpBinderFlags.ResultDiscarded,
            "WriteLine", null, typeof(Program),
            new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType,null),
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None,null)
            });
        SiteContainer.p__Site1 = CallSite<Action<CallSite, Type, object>>.Create(csb);
    }

    if (SiteContainer.p__Site2 == null)
    {
        CallSiteBinder csb = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
            CSharpBinderFlags.None, "Length", typeof(Program),
            new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
            });

        SiteContainer.p__Site2 = CallSite<Func<CallSite, object, object>>.Create(csb);
    }

    if (SiteContainer.p__Site4 == null)
    {
        CallSiteBinder csb = Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(
            CSharpBinderFlags.None, "Substring", null, typeof(Program),
            new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant | CSharpArgumentInfoFlags.UseCompileTimeType, null)    
            });
        SiteContainer.p__Site4 = CallSite<Func<CallSite, object, int, object>>.Create(csb);
    }

    var lengthResult = SiteContainer.p__Site2.Target(SiteContainer.p__Site2, obj1);
    SiteContainer.p__Site1.Target(SiteContainer.p__Site1, typeof(Console), lengthResult);


    var subStringResult = SiteContainer.p__Site4.Target(SiteContainer.p__Site4, obj1, 1);
    SiteContainer.p__Site1.Target(SiteContainer.p__Site1, typeof(Console), subStringResult);

    Console.ReadLine();
}


在這里,我沒有使用p__Site3,因為p__Site3和p__Site1相同,不過為什么微軟會生成4個CallSite<T>對象,因為1 和3是完全相同的,難道是為了實現簡單?? 、幸虧還有延遲初始化,否則靜態字段這么多,不知道會對系統產生什么影響 運行,
結果如下:

 clip_image002[4]

從這個例子也可以知道為什么dynamic會比反射的速度要快了。
1:if(p__Site1)==null,p__Site1==xxx,并且p__Site1是靜態類中的靜態字段,所以有緩存效果。
2:CallSite<T>.Target: 0 級緩存 - 基于站點歷史記錄專用的委托.
使用委托來調用,自然比反射又要快一些。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成人精精品一区二区频 | 蜜臀av性久久久久蜜臀aⅴ流畅 | 国产精品亚欧美一区二区 | 在线免费观看av片 | 成人在线播放 | 一区二区三区四区精品 | 亚洲国产欧美在线 | www.久久| 国产精品一区二区三区网站 | 青青草av | 国产精品久久久久久久久免费桃花 | 综合婷婷| 欧美成人一区二区三区片免费 | 欧美一性一交 | 97成人在线视频 | 国产精品久久久一区二区三区 | 国产精品色婷婷久久58 | 粉嫩av网站 | 正在播放国产一区二区 | 中文字幕第6页 | 国产成人免费在线视频 | 日本黄色大片免费 | 亚洲精品日韩综合观看成人91 | 日韩在线高清 | 久久久国产精品 | 日韩欧美国产一区二区三区 | 欧美日韩一区不卡 | 国产精品成人在线观看 | 成年人免费看 | av一区二区三区四区 | 欧美日韩电影一区 | 香蕉三级 | 日韩在线观看不卡 | 成人做爰9片免费视频 | 久久久青草婷婷精品综合日韩 | 成人二区 | 一区二区三区高清 | 日韩免费看 | 亚洲另类视频 | 日韩中文字幕在线 | t66y最新地址一地址二69 |