<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)獲取對象屬性值,這才是高效正確的姿勢!

          共 3975字,需瀏覽 8分鐘

           ·

          2021-01-11 20:20


          轉自: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; }
          }


          然后通過直接代碼調(diào)用方式來取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,性能要比反射好非常多:



          委托調(diào)用


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


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


          • 通過委托調(diào)用方法來取得屬性值


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


          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,性能幾乎與直接調(diào)用一致:



          最后做一個簡單的封裝,緩存一下創(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 -

          回復?【關閉】
          回復?【實戰(zhàn)】獲取20套實戰(zhàn)源碼
          回復?【被刪】
          回復?【訪客】
          回復?【小程序】學獲取15套【入門+實戰(zhàn)+賺錢】小程序源碼
          回復?【python】學微獲取全套0基礎Python知識手冊
          回復?【2019】獲取2019 .NET 開發(fā)者峰會資料PPT
          回復?【加群】加入dotnet微信交流群

          副業(yè)剛需,沒有人能拒絕這個網(wǎng)站!


          終于GitHub App 已支持簡體中文!


          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  波多野结衣av中文字幕 | 91精品综合久久久久久五月天 | 黄色一级网址 | 青操青青操逼网 | 黄色链接免费 |