Why does reflection not work well in .NET?

I am interested to know the technical reasons: why does reflection not work very well in .NET?

+5
source share
7 answers

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 ).

+2

. " " . . .NET . . .NET 1.x , , , .NET 2.0+ .

99% " " . , .

+16

, "Reflection" , . .NET , "". -, typeof() ... CLR . typeof() ( .) "" , , sizeof() .., ( , .)

, , , typeof(), , , , . - .NET, .

. . , , , , .

- , , , , , , (.. , - ..). , . , , , .

:

, , . System.Reflection.Emit, , DynamicMethod, , . , .

+5

, MSDN: Dodge Common Performance Pitfalls . , . , . ...

Base.Invoke, DynamicMethod Invoke, Type.InvokeMember ( Delegate.DynamicInvoke). , . , , , , .

. .

+4

( ) , / .

+1

tokens, (, , ... ..).

. 1- . 2- , ( ), .

, CLR , , , .

+1

.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.

0
source

All Articles