[ACCEPTED]-Use-cases for reflection-reflection

Accepted answer
Score: 21

I can list following usage for reflection:

  • Late binding
  • Security (introspect code for security reasons)
  • Code analysis
  • Dynamic typing (duck typing is not possible without reflection)
  • Metaprogramming

Some 3 real-world usages of reflection from my 2 personal experience:

  • Developed plugin system based on reflection
  • Used aspect-oriented programming model
  • Performed static code analysis
  • Used various Dependency Injection frameworks
  • ...

Reflection is good thing 1 :)

Score: 4

I've used reflection to get current method 3 information for exceptions, logging, etc.

string src = MethodInfo.GetCurrentMethod().ToString();
string msg = "Big Mistake";
Exception newEx = new Exception(msg, ex);
newEx.Source = src;

instead 2 of

string src = "MyMethod";
string msg = "Big MistakeA";
Exception newEx = new Exception(msg, ex);
newEx.Source = src;

It's just easier for copy/paste inheritance 1 and code generation.

Score: 3

I'm in a situation now where I have a stream 10 of XML coming in over the wire and I need 9 to instantiate an Entity object that will 8 populate itself from elements in the stream. It's 7 easier to use reflection to figure out which 6 Entity object can handle which XML element 5 than to write a gigantic, maintenance-nightmare 4 conditional statement. There's clearly a 3 dependency between the XML schema and how 2 I structure and name my objects, but I control 1 both so it's not a big problem.

Score: 2

There are lot's of times you want to dynamically 8 instantiate and work with objects where 7 the type isn't known until runtime. For 6 example with OR-mappers or in a plugin architecture. Mocking 5 frameworks use it, if you want to write 4 a logging-library and dynamically want to 3 examine type and properties of exceptions.

If 2 I think a bit longer I can probably come 1 up with more examples.

Score: 2

I find reflection very useful if the input 21 data (like xml) has a complex structure 20 which is easily mapped to object-instances 19 or i need some kind of "is a" relationship 18 between the instances.

As reflection is 17 relatively easy in java, I sometimes use 16 it for simple data (key-value maps) where 15 I have a small fixed set of keys. One one 14 hand it's simple to determine if a key is 13 valid (if the class has a setter setKey(String 12 data)), on the other hand i can change the 11 type of the (textual) input data and hide 10 the transformation (e.g simple cast to int 9 in getKey()), so the rest of the application 8 can rely on correctly typed data. If the 7 type of some key-value-pair changes for 6 one object (e.g. form int to float), i only 5 have to change it in the data-object and 4 its users but don't have to keep in mind 3 to check the parser too. This might not 2 be a sensible approach, if performance is 1 an issue...

Score: 1

Writing dispatchers. Twisted uses python's 8 reflective capabilities to dispatch XML-RPC 7 and SOAP calls. RMI uses Java's reflection 6 api for dispatch.

Command line parsing. Building 5 up a config object based on the command 4 line parameters that are passed in.

When 3 writing unit tests, it can be helpful to 2 use reflection, though mostly I've used 1 this to bypass access modifiers (Java).

Score: 1

I've used reflection in C# when there was 6 some internal or private method in the framework 5 or a third party library that I wanted to 4 access.

(Disclaimer: It's not necessarily 3 a best-practice because private and internal 2 methods may be changed in later versions. But 1 it worked for what I needed.)

Score: 1

Well, in statically-typed languages, you'd 9 want to use reflection any time you need 8 to do something "dynamic". It comes in 7 handy for tooling purposes (scanning the 6 members of an object). In Java it's used 5 in JMX and dynamic proxies quite a bit. And 4 there are tons of one-off cases where it's 3 really the only way to go (pretty much anytime 2 you need to do something the compiler won't 1 let you do).

Score: 1

I generally use reflection for debugging. Reflection 16 can more easily and more accurately display 15 the objects within the system than an assortment 14 of print statements. In many languages that 13 have first-class functions, you can even 12 invoke the functions of the object without 11 writing special code.

There is, however, a 10 way to do what you want(ed). Use a hashtable. Store 9 the fields keyed against the field name.

If 8 you really wanted to, you could then create 7 standard Get/Set functions, or create macros 6 that do it on the fly. #define GetX() Get("X") sort of thing.

You 5 could even implement your own imperfect 4 reflection that way.

For the advanced user, if 3 you can compile the code, it may be possible 2 to enable debug output generation and use 1 that to perform reflection.

More Related questions