Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of reflection in Java/C# etc [duplicate]

I was just curious, why should we use reflection in the first place?

// Without reflection
Foo foo = new Foo();
foo.hello();

// With reflection
Class cls = Class.forName("Foo");
Object foo = cls.newInstance();
Method method = cls.getMethod("hello", null);
method.invoke(foo, null);

We can simply create an object and call the class's method, but why do the same using forName, newInstance and getMthod functions?

To make everything dynamic?

like image 869
zengr Avatar asked Mar 21 '10 20:03

zengr


People also ask

What is reflection used for?

Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object. Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.

What is reflector in Java?

Refactoring simply means "improving the design of existing code without changing its observable behaviour".

Why is reflection useful programming?

Reflection helps programmers make generic software libraries to display data, process different formats of data, perform serialization or deserialization of data for communication, or do bundling and unbundling of data for containers or bursts of communication.


2 Answers

Simply put: because sometimes you don't know either the "Foo" or "hello" parts at compile time.

The vast majority of the time you do know this, so it's not worth using reflection. Just occasionally, however, you don't - and at that point, reflection is all you can turn to.

As an example, protocol buffers allows you to generate code which either contains full statically-typed code for reading and writing messages, or it generates just enough so that the rest can be done by reflection: in the reflection case, the load/save code has to get and set properties via reflection - it knows the names of the properties involved due to the message descriptor. This is much (much) slower but results in considerably less code being generated.

Another example would be dependency injection, where the names of the types used for the dependencies are often provided in configuration files: the DI framework then has to use reflection to construct all the components involved, finding constructors and/or properties along the way.

like image 52
Jon Skeet Avatar answered Nov 23 '22 08:11

Jon Skeet


It is used whenever you (=your method/your class) doesn't know at compile time the type should instantiate or the method it should invoke.

Also, many frameworks use reflection to analyze and use your objects. For example:

  • hibernate/nhibernate (and any object-relational mapper) use reflection to inspect all the properties of your classes so that it is able to update them or use them when executing database operations
  • you may want to make it configurable which method of a user-defined class is executed by default by your application. The configured value is String, and you can get the target class, get the method that has the configured name, and invoke it, without knowing it at compile time.
  • parsing annotations is done by reflection
like image 29
Bozho Avatar answered Nov 23 '22 07:11

Bozho