对对象类型和调用方法属性进行存储以提升反射性能
如果将对象的类型和方法属性进行一下存储,性能方法应该会得到改观.
简单的做了下测试:
{
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个方法:
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
对对象类型和调用方法属性进行存储以提升反射性能相关推荐
- 为什么基本类型可以调用方法——以字符串为例
引用类型中的基本包装类型 对于str.substring(2)这种方法我们经常使用, var str = "hello world"; var s1 = str.substring ...
- oracle对象类型的member方法
*:用于访问对象实例的数据.如果在对象类型中需要访问特定对象实例的数据,则需要定义member方法,member方法可以使用内置参数self访问当前实例对象,当定义member方法时,无论是否定义se ...
- 利用js的闭包原理做对象封装及调用方法
创建一个js文件,名为testClosure.js: ? 1 2 3 4 5 6 7 8 9 (function () { function a() { alert('i am a') ...
- python布尔类型运算_Python对象类型及其运算方法(详解)
基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = 'abc' >> ...
- 获取对象的接口信息(方法/属性/事件)(VB6代码)
要不是WS的超级绿豆提起这东西,我还不知道呢.... 真是个好玩意!!能列出一个对象的接口,包括方法,属性,事件.....微软直接做了啊. 记录一下,以后要用到时免得忘了: Option Explic ...
- 创建健壮的isArray()函数(JavaScript中判断对象类型的种种方法)
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- android 反射调用方法可不可以重载,使用Java进行反射投射和重载方法调度
请注意,所有代码都是一个简化示例,目的是仅传达我的问题的核心思想.它应该都经过轻微的编辑后编译并运行.使用Java进行反射投射和重载方法调度 我有几个类都实现了一个通用接口. public inter ...
- python使方法执行10次_Python提升程序性能的七个手段
1. 使用局部变量 尽量使用局部变量代替全局变量: 便于维护, 也可以避免不必要的资源浪费 使用局部变量替换模块名字空间的变量, 例如: ls = os.linesep. 一方面给可以提高程序性能, ...
- 17开年第一篇Activity之间传递ListT 以及T对象类型数据的方法
现在转眼都十三了,大家都工作了,自己确还没有在家里闲得慌于是就做个App,谁知道就遇到了Activity间传递实例以及List<T>数据的问题.网上同类型的文章很多,我写只是为了做个笔记预 ...
最新文章
- 在计算机科学中算法这个术语是,计算机科学与基础考试模拟一.docx
- Visual Studio 2019更新到16.2.2
- python diango_Django 安装
- MySQL8.0连接url
- 深度分析Java的ClassLoader机制(源码级别)
- REVERSE-PRACTICE-CTFSHOW-7
- web前端开发技术要求会什么
- Spring Cloud与微服务学习总结(1)——Spring Cloud及微服务入门
- iOS 系统爆 Bug!
- carve into_最新carve的用法和短语例句
- php中的脚本加速扩展opcache
- 编译原理教程_5 自底向上分析
- hashmap hash冲突怎么解决_对HashMap的思考及手写实现
- 【数学分析新讲 笔记】第一章 实数
- window出现msvcp100.dll缺失问题
- Vanishing Point Constrained Lane DetectionWith a Stereo Camera (IEEE 2017)
- 制作WINRE恢复光盘
- vue展示日历 考勤展示_基于element-ui的日历显示当月考勤情况
- 3GPP协议 25.105
- 三星移动硬盘不显示的解决
热门文章
- Golang——死锁、互斥锁、读写锁的实现
- Java代码块的基本使用
- linux进程map,LInux环境运行mapReduce程序
- mysql批量导入数据脚本_MySQL数据库批量导入脚本
- dataframe修改列名_python dataframe操作大全数据预处理过程(dataframe、md5)
- linux用户命令快捷链接,linux简单命令
- (一)Eureka搭建服务注册中心
- 循环队列CircleQueue的使用
- 互联网晚报 | 12月31日 星期五 | 滴滴发布上市后首份财报;商汤科技正式登陆港交所;我国高铁运营里程突破4万公里...
- python库_python使用ctypes库调用DLL动态链接库_python