Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does reflection not perform well in .NET?

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

like image 217
Malcolm Avatar asked Jul 22 '09 01:07

Malcolm


People also ask

Is it OK to use reflection in C#?

You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.

Why is reflection typically considered slow?

Reflection is slower Because it involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed.

Is .NET reflection slow?

It's common knowledge that reflection in . NET is slow, but why is that the case? This post aims to figure that out by looking at what reflection does under-the-hood.

What is reflection in asp net Why should we avoid them?

Reflection enables you to use code that was not available at compile time. . NET Reflection allows application to collect information about itself and also manipulate on itself. It can be used effectively to find all the types in an assembly and/or dynamically invoke methods in an assembly.


1 Answers

reflection does not perform well

That's a very loaded statement. "Perform well" is relative. Compared to static code, reflective calls do not perform quite as well. However, in nearly all cases, reflection in .NET is extremely fast. I cannot understate that enough. Reflection got a bad reputation from .NET 1.x days and perhaps other languages, but reflection in .NET 2.0+ is blisteringly quick.

In 99% of cases, the "is reflection too slow" is an irrelevant concern. I doubt you would need to bother measuring the performance impact of a reflective call vs a static one.

like image 143
Rex M Avatar answered Nov 10 '22 01:11

Rex M