Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What problems does reflection solve?

Tags:

c#

reflection

I went through all the posts on reflection but couldn't find the answer to my question.

What were the problems in the programming world before .NET reflection came and how it solved those problems?

Please explain with an example.

like image 971
Embedd_0913 Avatar asked Jun 24 '09 07:06

Embedd_0913


People also ask

How can reflection help you solve problems?

However, reflective thinking is most important in prompting learning during complex problem-solving situations because it provides students with an opportunity to step back and think about how they actually solve problems and how a particular set of problem solving strategies is appropriated for achieving their goal.

What are the benefits of reflection?

Reflection gives the brain an opportunity to pause amidst the chaos, untangle and sort through observations and experiences, consider multiple possible interpretations, and create meaning. This meaning becomes learning, which can then inform future mindsets and actions.

What is the purpose of reflection?

Reflecting helps you to develop your skills and review their effectiveness, rather than just carry on doing things as you have always done them. It is about questioning, in a positive way, what you do and why you do it and then deciding whether there is a better, or more efficient, way of doing it in the future.

What is the problem of reflection?

The problem surfaces when one tries to predict the behavior of an individual by the behavior of the group of which the individual is a member. The problem is likened to the image of a person reflected in a mirror.


1 Answers

It should be stated that .NET reflection isn't revolutionary - the concepts have been around in other framework.

Reflection in .NET has 2 facets:

Investigating type information

Without some kind of reflection / introspection API, it becomes very hard to perform things like serialization. Rather than having this provided at runtime (by inspecting the properties/fields/etc), you often need code-generation instead, i.e. code that explicitly knows how to serialize each of your types. Tedious, and painful if you want to serialize something that doesn't have a twin.

Likewise, there is nowhere to store additional metadata about properties etc, so you end up having lots of additional code, or external configuration files. Something as simple as being able to associate a friendly name with a property (via an attribute) is a huge win for UI code.

Metaprogramming

.NET reflection also provides a mechanism to create types (etc) at runtime, which is hugely powerful for some specific scenarios; the alternatives are:

  • essentially running a parser/logic tree at runtime (rather than compiling the logic at runtime into executable code) - much slower
  • yet more code generation - yay!
like image 191
Marc Gravell Avatar answered Sep 19 '22 14:09

Marc Gravell