What is Reflection property of a programming langu

2019-04-24 13:51发布

问题:

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?

回答1:

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.



回答2:

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).



回答3:

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.



回答4:

It is the ability of a programming langauge to adapt it's behaviour based upon runtime information. In the .Net/C# world this is used frequently. For example when serializing data to xml an attribute can be added to specify the name of the field in the resultant xml.



回答5:

This is probably a better question for programmers.stackexchange.com.

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

Back in my VB6 days there were some UI objects that had a Text property and others that had a Description (or something other than 'Text' anyway, I forget). It was a pain because I couldn't encapsulate code to deal with both kinds of objects the same way. With reflection I would have at least been able to look and see whether an object had a Text or a Description property.

Or sometimes objects might both have a Text property, but they derive from different base classes and don't have any interface applied to them. Again, it's hard to encapsulate code like this in a statically typed language without the help of reflection, but with reflection even a statically typed language can deal with it.