<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C#高性能動態(tài)獲取對象屬性值

          共 4227字,需瀏覽 9分鐘

           ·

          2020-12-18 14:56

          轉自:ZKEASOFT
          zkea.net/codesnippet/detail/csharp-fast-get-property-value.html

          動態(tài)獲取對象的性能值,這個在開發(fā)過程中經(jīng)常會遇到,這里我們探討一下如何高性能的獲取屬性值。為了對比測試,我們定義一個類People


          public class People
          {
          public string Name { get; set; }
          }


          然后通過直接代碼調用方式來取1千萬次看要花多少時間:


          private static void Directly()
          {
          People people = new People { Name = "Wayne" };
          Stopwatch stopwatch = Stopwatch.StartNew();
          for (int i = 0; i < 10000000; i++)
          {
          object value = people.Name;
          }
          stopwatch.Stop();
          Console.WriteLine("Directly: {0}ms", stopwatch.ElapsedMilliseconds);
          }


          大概花了37ms:



          反射


          通過反射來獲取對象的屬性值,這應該是大家常用的方式,但這種方式的性能比較差。接下來我們來看看同樣取1千萬次需要多少時間:


          private static void Reflection()
          {
          People people = new People { Name = "Wayne" };
          Type type = typeof(People);
          PropertyInfo property = type.GetProperty("Name");
          Stopwatch stopwatch = Stopwatch.StartNew();
          for (int i = 0; i < 10000000; i++)
          {
          object value = property.GetValue(people);
          }
          stopwatch.Stop();
          Console.WriteLine("Reflection: {0}ms", stopwatch.ElapsedMilliseconds);
          }


          大概花了1533ms,果然要慢很多:



          那既然反射慢,那還有沒有其它方式呢?


          動態(tài)構建Lambda


          我們知道可以動態(tài)構建Linq的Lambda表達式,然后通過編譯后得到一個委托,如果能動態(tài)構建返回屬性值的委托,就可以取到值了。所以我們想辦法構建一個像這樣的委托:


          Func getName = m => m.Name;


          接下來我們就通過Expression來構建:


          private static void Lambda()
          {
          People people = new People { Name = "Wayne" };
          Type type = typeof(People);
          var parameter = Expression.Parameter(type, "m");//參數(shù)m
          PropertyInfo property = type.GetProperty("Name");
          Expression expProperty = Expression.Property(parameter, property.Name);//取參數(shù)的屬性m.Name
          var propertyDelegateExpression = Expression.Lambda(expProperty, parameter);//變成表達式 m => m.Name
          var propertyDelegate = (Funcobject
          >)propertyDelegateExpression.Compile();//編譯成委托
          Stopwatch stopwatch = Stopwatch.StartNew();
          for (int i = 0; i < 10000000; i++)
          {
          object value = propertyDelegate.Invoke(people);
          }
          stopwatch.Stop();
          Console.WriteLine("Lambda:{0}ms", stopwatch.ElapsedMilliseconds);
          }


          然后我們測試一下,大概花了138ms,性能要比反射好非常多:



          委托調用


          雖然動態(tài)構建Lambda的性能已經(jīng)很好了,但還是更好嗎?畢竟比直接調用還是差了一些,要是能直接調用屬性的取值方法就好了。


          在C#中,可讀屬性都有一個對應的get_XXX()的方法,可以通過調用這個方法來取得對應屬性的值。可以使用System.Delegate.CreateDelegate創(chuàng)建一個委托來調用這個方法。


          • 通過委托調用方法來取得屬性值


          我們定義一個MemberGetDelegate的委托,然后通過它來調用取值方法:


          delegate object MemberGetDelegate(People p);
          private static void Delegate()
          {
          People people = new People { Name = "Wayne" };
          Type type = typeof(People);
          PropertyInfo property = type.GetProperty("Name");
          MemberGetDelegate memberGet = (MemberGetDelegate)System.Delegate.CreateDelegate(typeof(MemberGetDelegate), property.GetGetMethod());
          Stopwatch stopwatch = Stopwatch.StartNew();
          for (int i = 0; i < 10000000; i++)
          {
          object value = memberGet(people);
          }
          stopwatch.Stop();
          Console.WriteLine("Delegate: {0}ms", stopwatch.ElapsedMilliseconds);
          }


          然后我們測試一下,大概花了38ms,性能幾乎與直接調用一致:



          最后做一個簡單的封裝,緩存一下創(chuàng)建的Delegate


          public class PropertyValue
          {
          private static ConcurrentDictionary<string, MemberGetDelegate> _memberGetDelegate = new ConcurrentDictionary<string, MemberGetDelegate>();
          delegate object MemberGetDelegate(T obj);
          public PropertyValue(T obj)
          {
          Target = obj;
          }
          public T Target { get; private set; }
          public object Get(string name)
          {
          MemberGetDelegate memberGet = _memberGetDelegate.GetOrAdd(name, BuildDelegate);
          return memberGet(Target);
          }
          private MemberGetDelegate BuildDelegate(string name)
          {
          Type type = typeof(T);
          PropertyInfo property = type.GetProperty(name);
          return (MemberGetDelegate)Delegate.CreateDelegate(typeof(MemberGetDelegate), property.GetGetMethod());
          }
          }


          這樣使用起來就方便多了


          People people = new People { Name = "Wayne" };
          PropertyValue propertyValue = new PropertyValue(people);
          object value = propertyValue.Get("Name");


          - EOF -


          一些推薦

          1. 【Blazor 開源控件庫】點擊瀏覽

          2. 【B/S 開源項目】點擊瀏覽

          3. 【W(wǎng)PF 開源控件庫】點擊瀏覽

          4. 【W(wǎng)PF 開源項目】點擊瀏覽

          5. 【Xamarin 開源項目】點擊瀏覽

          6. 【W(wǎng)inform 開源控件庫】點擊瀏覽

          7. 【W(wǎng)inform 開源項目】點擊瀏覽

          8. 【Qt 開源控件庫】點擊瀏覽

          9. 【Qt 開源項目】點擊瀏覽

          10. 【更多分享】點擊瀏覽


          資源分享

          • 回復數(shù)字【01】:獲取DotNet技術資料
          • 回復數(shù)字【02】:獲取Java技術資料
          • 回復數(shù)字【03】:獲取Android技術資料
          • 回復數(shù)字【04】:獲取C++技術資料
          • 回復數(shù)字【05】:獲取Qt技術資料
          • 回復數(shù)字【06】:獲取React資源
          • 添加號主微信號【dotnet9】:備注【入群】加入與大佬們的技術交流
          • 添加QQ群【771992300】:備注【Dotnet9】加入技術交流,無人數(shù)上限、有資源共享

          ?

          時間如流水,只能流去不流回。

          • 公眾號:Dotnet9
          • 號主微信號:dotnet9
          • 編輯:沙漠之盡頭的狼
          • 日期:2020-12-14
          微信公眾號:Dotnet9
          -好東西要轉發(fā),設為"星標"★搶先看-

          點擊閱讀原文,關注Dotnet9更多好文。


          瀏覽 51
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人免费电影伊人大香蕉 | 成人 逼特逼视频 | 国产视频999 | 密桃视频黄片高清无码 | 日韩污视频 |