What is the Reflection property of a programming language?

It says that most dynamic-type languages ​​are reflective at a high level. Reflection (computer programming) on Wikipedia explains, but does not really provide a very clear picture of what this means. Can someone explain this in a simpler way with a suitable example?

+8
java reflection c # programming-languages
source share
5 answers

To give you an example of the practical use of Reflection:

Suppose you are developing an application that you want to extend with plugins. These plugins are simple assemblies containing only 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. This means that the content and logic must be loaded from another assembly when your application is already running. This means that these resources are not compiled into your assembly, i.e. MyApplication.exe. Suppose they are in the library: MyObjects.Person.dll.

Now you are faced with the fact that you will 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 can use System.Reflection to dynamically load resources in Runtime. This may help to understand how you can use it.

Take a look at the page to see examples of more detailed access to assemblies. This is basically the same content that I wrote.

+4
source share

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

The program can (1) check and (2) change its (a) own state / behavior or the state / behavior of the interoperator performing it (b).

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

  • 1a - Read the list of fields in which the object has
  • 2a β€” changing the value of one field based on the field name; reflective method call.
  • 1b - Inspect the current stack to find out what is the current method that is executing
  • 2b - Change the stack or how certain operations in the language are performed (for example, sending a message).

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

It is important to understand the concept of reification in order to understand reflection. When an instruction is computed in the interpreted program, the interpreter must represent it. The interpreter probably has objects for modeling the field, methods, etc. The program to be interpreted. After all, a translator is also a program. With reflection, the interpreted program can receive references to objects in the interpreter that represent their own structure. This is reification. (The next step will be to understand the causal relationship)

There are different types of reflective symptoms, and sometimes this confuses the understanding of what reflects or not, and what it means. Thinking in terms of program and interpreter. Hope this helps you understand the wikipedia page (which could be improved).

+2
source share

Reflection is the ability to request metadata that you wrote at runtime, for example: which classes are inside the assembly, which methods, fields and properties are contained in these classes, etc.

.net even contains β€œattributes”, these are classes that you can decorate with them with classes, methods, fields, etc. And their whole purpose is to add customized metadata that you can request at runtime.

Many time details depend only on metadata. During the check, we do not care about the string or int, but we make sure that it is not null. So, in this case, you need a property or attribute to check without caring for a particular class. A photograph is reflected there. And also, if you like to generate methods on the fly, (as available in the C # 4.0 dynamic object), than reflection can also be used. This mainly helps to do behavior or aspect-oriented programming.

Another popular use is a test environment. They use reflection to find test methods and run them in a proxy environment.

+1
source share

This is the ability of langauge programming to adapt its behavior based on runtime information. In the .Net / C # world, this is often used. For example, when serializing data in xml, you can add an attribute to specify the field name in the resulting xml.

0
source share

This is probably the best question for .stackexchange.com programmers.

But that basically means that you can look at your code from within your code.

In my days of VB6, there were some UI objects that had the Text property and others that had a description (or something other than "Text", anyway, I forgot). It was a pain because I could not encapsulate the code to deal with both types of objects equally. With reflection, I would at least be able to see if the object has a Text or Description property.

Or sometimes objects can have the Text property, but they come from different base classes and do not have any interface applied to them. Again, it is difficult to encapsulate such code into a statically typed language without the aid of reflection, but a statically typed language can even cope with reflection.

0
source share

All Articles