推荐一个快速反射调用的类

简介: 使用传统的.net反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览卢彦的博客时,找到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新实现了,反射调用方法,但是调用接口和.net原有方法一致。
使用传统的.net反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览 卢彦 的博客时,找到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新实现了,反射调用方法,但是调用接口和.net原有方法一致。而且调用时抛出的异常为所调用类的实际异常,不像传统方式返回为包装异常。
文章来源:
http://www.codeproject.com/csharp/FastMethodInvoker.asp

快速反射调用类
img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections.Generic;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Text;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Reflection;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Reflection.Emit;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
namespace  FastMethodInvoker
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    
class FastInvoke
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
public delegate object FastInvokeHandler(object target, object[] paramters);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
static object InvokeMethod(FastInvokeHandler invoke, object target, params object[] paramters)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
return invoke(null, paramters);
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public static FastInvokeHandler GetMethodInvoker(MethodInfo methodInfo)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            DynamicMethod dynamicMethod 
= new DynamicMethod(string.Empty, typeof(object), new Type[] img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.giftypeof(object), typeof(object[]) }, methodInfo.DeclaringType.Module);
img_33d02437d135341f0800e3d415312ae8.gif            ILGenerator il 
= dynamicMethod.GetILGenerator();
img_33d02437d135341f0800e3d415312ae8.gif            ParameterInfo[] ps 
= methodInfo.GetParameters();
img_33d02437d135341f0800e3d415312ae8.gif            Type[] paramTypes 
= new Type[ps.Length];
img_33d02437d135341f0800e3d415312ae8.gif            
for (int i = 0; i < paramTypes.Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (ps[i].ParameterType.IsByRef)
img_33d02437d135341f0800e3d415312ae8.gif                    paramTypes[i] 
= ps[i].ParameterType.GetElementType();
img_33d02437d135341f0800e3d415312ae8.gif                
else
img_33d02437d135341f0800e3d415312ae8.gif                    paramTypes[i] 
= ps[i].ParameterType;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            LocalBuilder[] locals 
= new LocalBuilder[paramTypes.Length];
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
for (int i = 0; i < paramTypes.Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                locals[i] 
= il.DeclareLocal(paramTypes[i], true);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
for (int i = 0; i < paramTypes.Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldarg_1);
img_33d02437d135341f0800e3d415312ae8.gif                EmitFastInt(il, i);
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldelem_Ref);
img_33d02437d135341f0800e3d415312ae8.gif                EmitCastToReference(il, paramTypes[i]);
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Stloc, locals[i]);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
if (!methodInfo.IsStatic)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldarg_0);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
for (int i = 0; i < paramTypes.Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (ps[i].ParameterType.IsByRef)
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldloca_S, locals[i]);
img_33d02437d135341f0800e3d415312ae8.gif                
else
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldloc, locals[i]);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
if (methodInfo.IsStatic)
img_33d02437d135341f0800e3d415312ae8.gif                il.EmitCall(OpCodes.Call, methodInfo, 
null);
img_33d02437d135341f0800e3d415312ae8.gif            
else
img_33d02437d135341f0800e3d415312ae8.gif                il.EmitCall(OpCodes.Callvirt, methodInfo, 
null);
img_33d02437d135341f0800e3d415312ae8.gif            
if (methodInfo.ReturnType == typeof(void))
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldnull);
img_33d02437d135341f0800e3d415312ae8.gif            
else
img_33d02437d135341f0800e3d415312ae8.gif                EmitBoxIfNeeded(il, methodInfo.ReturnType);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
for (int i = 0; i < paramTypes.Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (ps[i].ParameterType.IsByRef)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldarg_1);
img_33d02437d135341f0800e3d415312ae8.gif                    EmitFastInt(il, i);
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldloc, locals[i]);
img_33d02437d135341f0800e3d415312ae8.gif                    
if (locals[i].LocalType.IsValueType)
img_33d02437d135341f0800e3d415312ae8.gif                        il.Emit(OpCodes.Box, locals[i].LocalType);
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Stelem_Ref);
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            il.Emit(OpCodes.Ret);
img_33d02437d135341f0800e3d415312ae8.gif            FastInvokeHandler invoder 
= (FastInvokeHandler)dynamicMethod.CreateDelegate(typeof(FastInvokeHandler));
img_33d02437d135341f0800e3d415312ae8.gif            
return invoder;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
private static void EmitCastToReference(ILGenerator il, System.Type type)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
if (type.IsValueType)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Unbox_Any, type);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
else
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Castclass, type);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
private static void EmitBoxIfNeeded(ILGenerator il, System.Type type)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
if (type.IsValueType)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Box, type);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
private static void EmitFastInt(ILGenerator il, int value)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
switch (value)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
case -1:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_M1);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 0:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_0);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 1:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_1);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 2:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_2);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 3:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_3);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 4:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_4);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 5:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_5);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 6:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_6);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 7:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_7);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_33d02437d135341f0800e3d415312ae8.gif                
case 8:
img_33d02437d135341f0800e3d415312ae8.gif                    il.Emit(OpCodes.Ldc_I4_8);
img_33d02437d135341f0800e3d415312ae8.gif                    
return;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
if (value > -129 && value < 128)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldc_I4_S, (SByte)value);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
else
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                il.Emit(OpCodes.Ldc_I4, value);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif

效果测试程序
img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Reflection;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Reflection.Emit;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections.Generic;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Text;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Diagnostics;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
namespace  FastMethodInvoker
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    
class Program
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
static void Main(string[] args)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            Type t 
= typeof(Person);
img_33d02437d135341f0800e3d415312ae8.gif            MethodInfo methodInfo 
= t.GetMethod("Say");
img_33d02437d135341f0800e3d415312ae8.gif            Person person 
= new Person();
img_33d02437d135341f0800e3d415312ae8.gif            
string word = "hello";
img_33d02437d135341f0800e3d415312ae8.gif            Person p 
= null;
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
object[] param = new object[] img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{ word, p, 3 };
img_33d02437d135341f0800e3d415312ae8.gif            
int TestTimes = 100000//测试次数,可自行调节看效果
img_33d02437d135341f0800e3d415312ae8.gif

img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif            
传统方式反射#region 传统方式反射
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Stopwatch watch 
= new Stopwatch();
img_33d02437d135341f0800e3d415312ae8.gif                watch.Start();
img_33d02437d135341f0800e3d415312ae8.gif                
for (int i = 0; i < TestTimes; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    methodInfo.Invoke(person, param);
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_33d02437d135341f0800e3d415312ae8.gif                watch.Stop();
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(TestTimes.ToString() 
+ " times invoked by Reflection: " + watch.ElapsedMilliseconds + "ms");
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(
"传统方式反射 直接错误:" + ex.Message);
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(
"传统方式反射 内部错误:" + ex.InnerException.Message);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            
#endregion

img_33d02437d135341f0800e3d415312ae8.gif
img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif            
快速反射#region 快速反射
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Stopwatch watch1 
= new Stopwatch();
img_33d02437d135341f0800e3d415312ae8.gif                FastInvoke.FastInvokeHandler fastInvoker 
= FastInvoke.GetMethodInvoker(methodInfo);
img_33d02437d135341f0800e3d415312ae8.gif                watch1.Start();
img_33d02437d135341f0800e3d415312ae8.gif                
for (int i = 0; i < TestTimes; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    fastInvoker(person, param);
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_33d02437d135341f0800e3d415312ae8.gif                watch1.Stop();
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(TestTimes.ToString() 
+ " times invoked by FastInvoke: " + watch1.ElapsedMilliseconds + "ms");
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(
"快速反射 错误:" + ex.Message);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            
#endregion

img_33d02437d135341f0800e3d415312ae8.gif
img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif            
直接调用#region 直接调用
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Stopwatch watch2 
= new Stopwatch();
img_33d02437d135341f0800e3d415312ae8.gif                watch2.Start();
img_33d02437d135341f0800e3d415312ae8.gif                
for (int i = 0; i < TestTimes; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    person.Say(
ref word, out p, 3);
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_33d02437d135341f0800e3d415312ae8.gif                watch2.Stop();
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(TestTimes.ToString() 
+ " times invoked by DirectCall: " + watch2.ElapsedMilliseconds + "ms");
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Console.WriteLine(
"直接调用 错误:" + ex.Message);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            
#endregion

img_33d02437d135341f0800e3d415312ae8.gif            
img_33d02437d135341f0800e3d415312ae8.gif            Console.ReadLine();
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
public class Person
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
public void Say(ref string word, out Person p, int avi)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            word 
= "ttt" + avi.ToString();
img_33d02437d135341f0800e3d415312ae8.gif            p 
= new Person();
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
//throw new System.Exception("出错了哦");
img_105a1e124122b2abcee4ea8e9f5108f3.gif
        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
目录
相关文章
|
6月前
|
Java
【反射】Java反射机制 -- 常用构造器与方法
【反射】Java反射机制 -- 常用构造器与方法
60 0
|
6月前
调用反射类的指定方法
调用反射类的指定方法
|
6月前
调用反射类的方法
调用反射类的方法
|
6月前
深入类的方法
深入类的方法
|
Java API
Java反射(通过反射获取构造函数、方法、属性)
1.通过反射获取构造函数,2.通过反射获取方法,3.通过反射调用成员属性
114 0
|
IDE 安全 Java
Java 反射机制详解:私有方法调用头大?如何通过反射调用类中的私有方法?
Java 反射机制详解:私有方法调用头大?如何通过反射调用类中的私有方法?
269 0
Java 反射机制详解:私有方法调用头大?如何通过反射调用类中的私有方法?
|
XML Java 数据格式
反射机制(什么是反射、反射操作构造器、方法、字段、反射加载资源)
反射机制(什么是反射、反射操作构造器、方法、字段、反射加载资源)
157 0
反射机制(什么是反射、反射操作构造器、方法、字段、反射加载资源)
|
Java 开发者
反射调用普通方法|学习笔记
快速学习反射调用普通方法
114 0
反射调用普通方法|学习笔记
如何使用反射获取类对象
如何使用反射获取类对象
126 0
如何使用反射获取类对象
下一篇
无影云桌面