Reflection, because the compiler to determine permissions, identity and other operations, so it is slower than direct operation
Test code:
Const intNUM =1000000; Stopwatch SW=NewStopwatch (); Sw. Start (); for(inti =0; i < NUM; i++) {Reflecttest rt=Newreflecttest (); Rt. Countsomething (); } SW. Stop (); Console.WriteLine (SW. Elapsedticks); Sw. Reset (); Sw. Start (); for(inti =0; i < NUM; i++) {Assembly Assembly=assembly.getexecutingassembly (); Type T= assembly. GetType ("consoleapplication1.reflecttest"); ConstructorInfo ConstructorInfo= T.getconstructors (). (c =c.getparameters (). Length). FirstOrDefault (); Objectobj = Constructorinfo.invoke (New Object[Constructorinfo.getparameters (). Length]); //Object obj = activator.createinstance (t);MethodInfo MethodInfo = T.getmethod ("countsomething"); Methodinfo.invoke (obj,New Object[Methodinfo.getparameters (). Length]); } SW. Stop (); Console.WriteLine (SW. Elapsedticks); Console.readkey ();
Results:
Reflection time is 2 to 3 times times the direct call, so non-special needs, the project do not use reflection bar ...
C #, the efficiency difference between reflection and direct invocation