日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对对象类型和调用方法属性进行存储以提升反射性能

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对对象类型和调用方法属性进行存储以提升反射性能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
反射的性能差是一個公認的事實.而最耗性能的還是根據程序集獲取要調用的對象,而在對象里搜索要調用的方法所耗性能到不不是很多,如果對象里的方法不是特別的多,而去可以指定相關參數提高搜索的效率,比如BindingFlags,Binder.
如果將對象的類型和方法屬性進行一下存儲,性能方法應該會得到改觀.
簡單的做了下測試:
??? class?Program
????{
????????
static?IDictionary<string?,?Type>?typeList?=?new?Dictionary<string?,?Type>();

????????
static?IDictionary<string?,?MethodInfo>?methodList?=?new?Dictionary<string?,?MethodInfo>();

????????
static?void?Main?(?string[]?args?)
????????{
????????????
int?iBegin?=?Environment.TickCount;
????????????
for?(?int?i?=?0?;?i?<?1000?;?i++?)?
????????????{
????????????????Test1(?
"Test.TestClass,Test"?);
????????????????Test1(?
"Test1.Class1,Test1"?);??
????????????}
????????????Console.WriteLine(?
"普通:{0}"?,?Environment.TickCount?-?iBegin?);
????????????
????????????GC.Collect();
????????????iBegin?
=?Environment.TickCount;
????????????
for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test2(?
"Test.TestClass,Test"?);?????
????????????????Test2(?
"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?
"存儲Type:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();

????????????iBegin?
=?Environment.TickCount;
????????????
for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test3(?
"Test.TestClass,Test"?);
????????????????Test3(?
"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?
"存儲MethodInfo:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();

????????????Console.ReadLine();
????????}

????????
static?void?Test1?(?string?typeName?)
????????{
????????????Type?type?
=?Type.GetType(?typeName?);
????????????
object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?
=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?
null?);
????????????
????????????instance?
=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?
=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?
new?object[]?{?1?,?2?}?);
????????}

????????
//存儲Type
????????static?void?Test2?(?string?typeName?)
????????{
????????????Type?type?
=?GetType(?typeName?);

????????????
object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?
=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?
null?);

????????????instance?
=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?
=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?
new?object[]?{?1?,?2?}?);
????????}

????????
//存儲MethodInfo
????????static?void?Test3?(?string?typeName?)
????????{
????????????Type?type?
=?GetType(?typeName?);
????????????
object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?
=?GetMethod(?typeName?,?"GetValue"?,?type?,?new?Type[0]?);
????????????methodInfo.Invoke(?instance?,?
null?);

????????????instance?
=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?
=?GetMethod(?typeName?,?"Add"?,?type?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?
new?object[]?{?1?,?2?}?);
????????}

????????
static?Type?GetType?(?string?typeName?)
????????{
????????????Type?type?
=?null;
????????????
if?(?!typeList.ContainsKey(?typeName?)?)
????????????{
????????????????type?
=?Type.GetType(?typeName?);
????????????????typeList.Add(?typeName?,?type?);
????????????}
????????????
else
????????????????type?
=?typeList[typeName];
????????????
return?type;
????????}

????????
static?MethodInfo?GetMethod?(?string?typeName?,?string?methodName?,?Type?type?,?params?Type[]?types?)
????????{
????????????MethodInfo?methodInfo?
=?null;
????????????
if?(?!methodList.ContainsKey(?typeName?+?methodName?)?)
????????????{
????????????????methodInfo?
=?type.GetMethod(?methodName?,?types?);
????????????????methodList.Add(?typeName?
+?methodName?,?methodInfo?);
????????????}
????????????
else
????????????????methodInfo?
=?methodList[typeName?+?methodName];
????????????
return?methodInfo;
????????}????????
????}
其中 Test.TestClass 類和 Test1.Class1 都有一個無參構造函數和一個int類型的參數及2個方法:
int?val?=?0;
public?Class1?(?int?val?)
{
?????
this.val?=?val;
}
public?string?GetValue?()
{
?????
return?string.Format(?"the?value?is?{0}"?,?val?);
}
public?int?Add?(?int?a?,?int?b?)
{
?????
return?a?+?b;
}
為了測試,上面2個類里都加了幾個方法(只是定義,并沒有具體的實現代碼).
測試了下不進行任何處理,時間都在130左右波動,對Type進行存儲,時間在32左右,基本上沒什么波幅,而對Type和MethodInfo進行存儲時間維持在31.
看來這樣處理下對性能的改善還是起到了作用.

轉載于:https://www.cnblogs.com/doll-net/archive/2008/04/25/Reflection.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的对对象类型和调用方法属性进行存储以提升反射性能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。