Namespace ConsoleApplication {using System; using System. reflection; using System. reflection. emit; using System. collections. generic; // using System. text; using System. linq; using System. diagnostics; // using System. linq. expressions; using Microshaoft; /// <summary> /// summary of Class1. /// </Summary> public class Class1 {/// <summary> /// main entry point of the application. /// </Summary> // [STAThread] static void Main (string [] args) {Person person = new Person (); string word = "hello "; person p = null; object [] param = new object [] {word, p, 3}; Stopwatch wattings = new Stopwatch (); // FastInvokeHandler fastInvoker = DynamicMethodHelper. getDynamicMethodInvoker (methodInfo); Person x = null; int y =-1; DynamicMethodHelper. dynamicMethodFuncInvokeHandler handler1 = Dyn AmicMethodHelper. GetDynamicMethodFuncInvoker (person, mi => {return! Mi. isStatic & mi. name. equals ("Say") ;}); DynamicMethodHelper. dynamicMethodFuncInvokeHandler handler2 = DynamicMethodHelper. getDynamicMethodFuncInvoker <Person> (mi => {return mi. isStatic & mi. name. equals ("Say ");});//. invoke (person, new object [] {"hihi"}); CodeTimer. time ("serial test", 1, () => {var parameters = new object [] {"aaa", x, y}; handler1.DynamicInvoke (person, parameters); Console. WriteLine ("instance method Say Invoke ref word: {0}, int avi {1}", parameters [0], parameters [2]); handler1.Invoke (person, parameters ); console. writeLine ("instance method Say Invoke ref word: {0}, int avi {1}", parameters [0], parameters [2]); string s = (string) handler2.DynamicInvoke (person, new object [] {"bb", x, y}); Console. writeLine ("static method Say DynamicInvoke return: {0}", s); s = (string) handler2.Invoke (person, new o Bject [] {"bb", x, y}); Console. writeLine ("static method Say Invoke return: {0}", s) ;}); CodeTimer. parallelTime ("parallel test", 1, () => {var parameters = new object [] {"aaa", x, y}; handler1.DynamicInvoke (person, parameters); Console. writeLine ("instance method Say Invoke ref word: {0}, int avi {1}", parameters [0], parameters [2]); handler1.Invoke (person, parameters ); console. writeLine ("instance method Say Invoke ref word: {0}, Int avi {1} ", parameters [0], parameters [2]); string s = (string) handler2.DynamicInvoke (person, new object [] {" bb ", x, y}); Console. writeLine ("static method Say DynamicInvoke return: {0}", s); s = (string) handler2.Invoke (person, new object [] {"bb", x, y}); Console. writeLine ("static method Say Invoke return: {0}", s) ;}); Console. readLine () ;}} public class Person {public void Say (ref string word, out Person P, int avi) {word = "instance" + avi. toString (); // Console. writeLine (word); p = new Person (); // return 100;} public static string Say (string word) {// Console. writeLine ("Static Say"); return "return static" + word ;}} namespace Microshaoft {using System; using System. diagnostics; using System. threading; using System. threading. tasks; using System. runtime. interopServices; public static class Co DeTimer {public static void Initialize () {Process. getCurrentProcess (). priorityClass = ProcessPriorityClass. high; Thread. currentThread. priority = ThreadPriority. highest; Time ("", 1, () =>{});} public static void ParallelTime (string name, int iteration, Action action) {InternalIterationProcess (name, iteration, () => {Parallel. for (0, iteration, new ParallelOptions () {MaxDegreeOfParall Elism = 4, TaskScheduler = null}, I =>{ action () ;});} private static void InternalIterationProcess (string name, int iteration, Action action) {if (string. isNullOrEmpty (name) {return;} // 1. lelecolor currentForeColor = Console. foregroundColor; Console. foregroundColor = ConsoleColor. yellow; Console. writeLine (name); // 2. GC. collect (GC. maxGeneration, GCCollectionMode. forced); int [] GcCounts = new int [GC. maxGeneration + 1]; for (int I = 0; I <= GC. maxGeneration; I ++) {gcCounts [I] = GC. collectionCount (I);} // 3. stopwatch watch = new Stopwatch (); watch. start (); ulong cycleCount = GetCycleCount (); action (); ulong cpuCycles = GetCycleCount ()-cycleCount; watch. stop (); // 4. console. foregroundColor = currentForeColor; Console. writeLine ("\ tTime Elapsed: \ t" + watch. elapsedMilli Seconds. toString ("N0") + "ms"); Console. writeLine ("\ tCPU Cycles: \ t" + cpuCycles. toString ("N0"); // 5. for (int I = 0; I <= GC. maxGeneration; I ++) {int count = GC. collectionCount (I)-gcCounts [I]; Console. writeLine ("\ tGen" + I + ": \ t" + count);} Console. writeLine ();} public static void Time (string name, int iteration, Action action) {InternalIterationProcess (name, iteration, () => {fo R (int I = 0; I <iteration; I ++) {action () ;}});} private static ulong GetCycleCount () {ulong cycleCount = 0; queryThreadCycleTime (GetCurrentThread (), ref cycleCount); return cycleCount;} [DllImport ("kernel32.dll")] [return: financialas (UnmanagedType. bool)] static extern bool QueryThreadCycleTime (IntPtr threadHandle, ref ulong cycleTime); [DllImport ("kernel32.dll")] static extern IntPtr GetCu RrentThread () ;}} namespace Microshaoft {using System; using System. reflection; using System. reflection. emit; using System. collections. generic; using System. text; using System. linq; using System. diagnostics; public class DynamicMethodHelper {public delegate object DynamicMethodFuncInvokeHandler (object target, object [] parameters); public static DynamicMethodFuncInvokeHandler GetDynamicMethodFuncIn Voker (string typeName, Func <MethodInfo, bool> predicateFunc) {MethodInfo methodInfo = Type. getType (typeName ). getMethods (). first (mi =>{ return predicateFunc (mi) ;}); DynamicMethod dynamicMethod = GetDynamicMethod (methodInfo); return (DynamicMethodFuncInvokeHandler) dynamicMethod. createDelegate (typeof (DynamicMethodFuncInvokeHandler);} public static DynamicMethodFuncInvokeHandler GetDynamicMeth OdFuncInvoker <TTarget> (Func <MethodInfo, bool> predicateFunc) {MethodInfo methodInfo = typeof (TTarget ). getMethods (). first (mi =>{ return predicateFunc (mi) ;}); DynamicMethod dynamicMethod = GetDynamicMethod (methodInfo); return (DynamicMethodFuncInvokeHandler) dynamicMethod. createDelegate (typeof (DynamicMethodFuncInvokeHandler);} public static DynamicMethodFuncInvokeHandler GetDynamicMethodFuncI Nvoker (object target, Func <MethodInfo, bool> predicateFunc) {MethodInfo methodInfo = target. getType (). getMethods (). first (mi =>{ return predicateFunc (mi) ;}); DynamicMethod dynamicMethod = GetDynamicMethod (methodInfo); return (DynamicMethodFuncInvokeHandler) dynamicMethod. createDelegate (typeof (DynamicMethodFuncInvokeHandler);} private static DynamicMethod GetDynamicMethod (MethodInfo methodInf O) {int I = 0; var list = methodInfo. getParameters (). toList (); DynamicMethod dynamicMethod = new DynamicMethod (string. empty, typeof (object), new Type [] {typeof (object), typeof (object [])}, methodInfo. declaringType. module); ILGenerator ilg = dynamicMethod. getILGenerator (); if (! MethodInfo. isStatic) {ilg. emit (OpCodes. ldarg_0);} I = 0; list. forEach (pi => {Type pt; if (pi. parameterType. isByRef) {pt = pi. parameterType. getElementType ();} else {pt = pi. parameterType;} // paramTypes [I] = pt; LocalBuilder lb = ilg. declareLocal (pt, true); ilg. emit (OpCodes. ldarg_1); EmitFastInt (ilg, I); ilg. emit (OpCodes. ldelem_Ref); EmitCastToReference (ilg, pt); ilg. emit (OpCodes. stloc, lb); if (pi. parameterType. isByRef) {ilg. emit (OpCodes. ldloca_S, lb); ilg. emit (OpCodes. ldarg_1); EmitFastInt (ilg, I); ilg. emit (OpCodes. ldloc, lb); if (lb. localType. isValueType) {ilg. emit (OpCodes. box, lb. localType);} ilg. emit (OpCodes. stelem_Ref);} else {ilg. emit (OpCodes. ldloc, lb) ;} I ++ ;}); if (methodInfo. isStatic) {ilg. emitCall (OpCodes. call, methodInfo, null);} else {ilg. emitCall (OpCodes. callvirt, methodInfo, null);} if (methodInfo. returnType = typeof (void) {ilg. emit (OpCodes. ldnull);} else {EmitBoxIfNeeded (ilg, methodInfo. returnType);} ilg. emit (OpCodes. ret); return dynamicMethod;} private static void EmitCastToReference (ILGenerator ilg, Type) {if (type. isValueType) {ilg. emit (OpCodes. unbox_Any, type);} else {ilg. emit (OpCodes. castclass, type) ;}} private static void EmitBoxIfNeeded (ILGenerator ilg, Type type) {if (type. isValueType) {ilg. emit (OpCodes. box, type) ;}} private static void EmitFastInt (ILGenerator il, int value) {switch (value) {case-1: il. emit (OpCodes. ldc_I4_M1); return; case 0: il. emit (OpCodes. ldc_I4_0); return; case 1: il. emit (OpCodes. ldc_I4_1); return; case 2: il. emit (OpCodes. ldc_I4_2); return; case 3: il. emit (OpCodes. ldc_I4_3); return; case 4: il. emit (OpCodes. ldc_I4_4); return; case 5: il. emit (OpCodes. ldc_I4_5); return; case 6: il. emit (OpCodes. ldc_I4_6); return; case 7: il. emit (OpCodes. ldc_I4_7); return; case 8: il. emit (OpCodes. ldc_I4_8); return;} if (value>-129 & value <128) {il. emit (OpCodes. ldc_I4_S, (SByte) value);} else {il. emit (OpCodes. ldc_I4, value );}}}} |