I am interested to know the technical reasons: why does reflection not work very well in .NET?
Reflection works well, it's just a lot more than static code.
Say you have this piece of code:
typeof(SomeClass).GetMethod("SomeStaticMethod"). Invoke(null, new object[] { 1, 2, 3 });
This is the same as:
SomeClass.SomeStaticMethod(1, 2, 3);
But it should be obvious that the former has a lot more work. He should get information about the type, cross it to see if there is a SomeStaticMethod method, check which method it is, call the method on the instance or not, if it is static and pass an array of objects - these are the parameters, boxing / unpacking of integers and this case.
, , , , . , , - : WinForms ASP.NET MVC ( , , MVC, , , MVC ).
. " " . . .NET . . .NET 1.x , , , .NET 2.0+ .
99% " " . , .
, "Reflection" , . .NET , "". -, typeof() ... CLR . typeof() ( .) "" , , sizeof() .., ( , .)
typeof()
sizeof()
, , , typeof(), , , , . - .NET, .
. . , , , , .
- , , , , , , (.. , - ..). , . , , , .
:
, , . System.Reflection.Emit, , DynamicMethod, , . , .
, MSDN: Dodge Common Performance Pitfalls . , . , . ...
Base.Invoke, DynamicMethod Invoke, Type.InvokeMember ( Delegate.DynamicInvoke). , . , , , , .
. .
( ) , / .
tokens, (, , ... ..).
. 1- . 2- , ( ), .
, CLR , , , .
.NET - , .
, , . , , , .., .
This does not mean that it is really slow - it is just LOT slower than fully compiled scroll method calls and search queries. It should be - but I really think more about it, since compiled code is faster than dynamically searching for information on any system.