Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Reflection property of a programming language?

Its said that most high-level dynamically types languages are reflexive. Reflection (computer programming) on Wikipedia explains but it doesn't really give a very clear picture of what it means. Can anyone explain it in a simpler way by a relevant example?

like image 884
Tirth Pandya Avatar asked Jan 24 '13 15:01

Tirth Pandya


3 Answers

To give you a example how to use Reflection in a practical way:

Let's assume you are developing an Application which you'd like to extend using plugins. These plugins are simple Assemblies containing just a class named Person:

namespace MyObjects
{
    public class Person
    {
        public Person() { ... Logic setting pre and postname ... }
        private string _prename;
        private string _postname;
        public string GetName() { ... concat variabes and return ... }
    }
}

Well, plugins should extend your application at runtime. That means, that the content and logic should be loaded from another assembly when your application already runs. This means that these resources are not compiled into your Assembly, i.e. MyApplication.exe. Lets assume they are located in a library: MyObjects.Person.dll.

You are now faced with the fact that you'll need to extract this Information and for example access the GetName() function from MyObjects.Person.

// Create an assembly object to load our classes
Assembly testAssembly = Assembly.LoadFile(Application.StartUpPath + @"MyObjects.Person.dll");
Type objType = testAssembly.GetType("MyObjects.Person");

// Create an instace of MyObjects.Person
var instance = Activator.CreateInstance(objType);

// Call the method
string fullname = (string)calcType.InvokeMember("GetName",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, instance, null);

As you can see, you could use System.Reflection for dynamic load of Resources on Runtime. This might be a help understanding the ways you can use it.

Have a look on this page to see examples how to access assemblys in more detail. It's basically the same content i wrote.

like image 129
Dennis Alexander Avatar answered Oct 22 '22 14:10

Dennis Alexander


To better understand reflection, think of an interpreter that evaluates a program. The interpreter is a program that evaluates other programs.

The program can (1) inspect and (2) modify its (a) own state/behavior, or the state/behavior of the interperter running it (b).

There are then four combinations. Here is an example of each kind of action:

  • 1a -- Read the list of fields an object has
  • 2a -- Modification of the value of one field based on the name of the field; reflective invocation of methods.
  • 1b -- Inspect the current stack to know what is the current method that is executed
  • 2b -- Modify the stack or how certain operations in the language are executed (e.g. message send).

Type a is called structural reflection. Type b is called behavioral reflection. Reflection of type a is fairly easy to achieve in a language. Reflection of type b is way more complicated, especially 2b--this is an open research topic. What most people understand by reflection is 1a and 2a.

It is important to understand the concept of reification to understand reflection. When a statement in the program that is interpreted is evaluated, the interpreter needs to represent it. The intepreter has probably objects to model field, methods, etc. of the program to be interpreted. After all, the interpreter is a program as well. With reflection, the interpreted program can obtain references to objects in the interpreter that represent its own structure. This is reification. (The next step would be to understand causal connection)

There are various kinds of reflective features and it's sometimes confusing to understand what's reflective or not, and what it means. Thinking in term of program and interpreter. I hope it will help you understand the wikipedia page (which could be improved).

like image 26
ewernli Avatar answered Oct 22 '22 15:10

ewernli


Reflection is the ability to query the metadata the program that you wrote in run-time, For example : What classes are found inside an assembly, What methods, fields and properties those classes contains, and more.

.net contains even 'attributes', those are classes that you can decorate with them classes, methods, fields and more, And all their purpose is to add customized metadata that you can query in run-time.

Many time details depend on metadata only. At the time of validation we don't care about string or int but we care that it should not be null. So, in that case you need a property or attribute to check without caring about specific class. There reflection comes in picture. And same way if you like to generate methods on a fly, (as available in dynamic object of C# 4.0), than also it is possible using reflection. Basically it help to do behavior driven or aspect oriented programming.

Another popular use is Testing framework. They use reflection to find methods to test and run it in proxy environment.

like image 2
Shani Elharrar Avatar answered Oct 22 '22 14:10

Shani Elharrar