What exactly is Reflection and when is it a good approach?

前端 未结 11 1669
天命终不由人
天命终不由人 2020-12-25 14:58

What exactly is Reflection? I read the Wikipedia article on this subject and I understand that it is a kind of meta-programming, where the program can modify itself at run-

相关标签:
11条回答
  • 2020-12-25 15:11

    It's not so much modifying code at execution time, but examining objects and asking them to execute code without knowing their type statically.

    One simple way of describing it would be "a somewhat painful way of making a statically typed language behave dynamically."

    EDIT: Uses:

    • Configuration (e.g. take an XML file which specifies types and properties, then construct appropriate objects)
    • Testing (unit tests which are identified by name or attributes)
    • Web services (in .NET at least, a lot of reflection is used in the core web service engine)
    • Automatic event wiring - provide a method with an appropriate name, e.g. SubmitButton_Click and ASP.NET will attach that method as a handler for the SubmitButton's Click event (if you have autowiring turned on)

    Is it a good idea? Well, only when the alternatives are painful. I prefer static typing when it doesn't get in the way - then you get lots of compile-time goodness, and it's faster too. But when you do need it, reflection lets you do various things which otherwise just wouldn't be possible.

    0 讨论(0)
  • 2020-12-25 15:14

    In Java it is basically a way to instantiate a class without knowing about it before hand. Say you want the user to be able to change a configuration file by adding the class they want your program to use (say you have numerous implementations of some interface). With reflection you can create an object based on just it's name, method signature, etc. . . and then cast it to your interface.

    0 讨论(0)
  • 2020-12-25 15:16

    The first good example I can think of off the top of my head is for when you need to execute a set of methods on a given object without knowing at compile-time what methods will exist in it.

    Take unit testing frameworks for example. The test runner class that is responsible for running all your unit tests doesn't know ahead of time what you are going to name your methods. All it knows is that they will be prefixed with "test" (or in Java 5's case, annotated with @Test). So when given a test class, it reflects on that class in order to get a list of all the methods in it. Then, it iterates through those method names as strings and calls those methods on the object if they start with "test". That wouldn't be possible without reflection. And that's just one example.

    0 讨论(0)
  • 2020-12-25 15:23

    Reflection is a facility where you can query an object about its attributes at runtime. For example, Python, Java and .Net have facilities where you can find the instance variables or methods of an object.

    An example of an application for reflection is an O/R mapping layer. Some use reflection to construct an object by quering its properties at runtime and dynamically populating an instance. This allows you to do this programatically based on metadata from some sort of data dictionary without having to recompile the application.

    To take a simple example, I'll use Python because its reflection facilities are very simple to use and involve less boilerplate than those of java or .Net.

    ActivePython 2.5.2.2 (ActiveState Software Inc.) based on
    Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class foo:
    ...     def __init__(self):
    ...             self.x = 1
    ...
    >>> xx = foo()      # Creates an object and runs the constructor
    >>> xx.__dict__     # System metadata about the object
    {'x': 1}
    >>> a = xx.__dict__ # Now we manipulate the object through 
    >>> a['y'] = 2      # its metadata ...
    >>> print xx.y      # ... and suddenly it has a new instance variable
    2                   
    >>>
    

    Now, we've used basic reflection to examine the instance variables of an arbitrary object. The special variable __dict__ on Python is a system property of an object that has a hash table of its members keyed by the variable (or method) name. We have reflectively examined the object and used the reflection facilities to artificially poke a second instance variable into it, which we can then display by invoking it as an instance variable.

    Note that this particular trick doesn't work on Java or .Net, as the instance variables are fixed. The type system of these languages doesn't allow new instance variables to be added at runtime in the way that python's 'duck' typing system does. However, you could have reflectively updated the value of an instance variable that was declared in the type definition.

    You can also use reflection to dynamically construct method invocations and perform various other neat tricks such as instantiating an object based on a parameter. For example, if you had some sort of plugin based system where certain capabilities were optional, you could use reflection to query the plugin about what services it offered (perhaps by querying whether certain interfaces were implemented) without requiring explicit metadata.

    Many dynamic language interfaces such as OLE automation use reflection as an integral part of the interface.

    0 讨论(0)
  • 2020-12-25 15:24

    Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. Typical uses of reflection include the following:

    • Use Assembly to define and load assemblies, load modules that are listed in the assembly manifest, and locate a type from this assembly and create an instance of it.
    • Use Module to discover information such as the assembly that contains the module and the classes in the module. You can also get all global methods or other specific, nonglobal methods defined on the module.
    • Use ConstructorInfo to discover information such as the name, parameters, access modifiers (such as public or private), and implementation details (such as abstract or virtual) of a constructor. Use the GetConstructors or GetConstructor method of a Type to invoke a specific constructor.
    • Use MethodInfo to discover information such as the name, return type, parameters, access modifiers (such as public or private), and implementation details (such as abstract or virtual) of a method. Use the GetMethods or GetMethod method of a Type to invoke a specific method.
    • Use FieldInfo to discover information such as the name, access modifiers (such as public or private) and implementation details (such as static) of a field, and to get or set field values.
    • Use EventInfo to discover information such as the name, event-handler data type, custom attributes, declaring type, and reflected type of an event, and to add or remove event handlers.
    • Use PropertyInfo to discover information such as the name, data type, declaring type, reflected type, and read-only or writable status of a property, and to get or set property values.
    • Use ParameterInfo to discover information such as a parameter's name, data type, whether a parameter is an input or output parameter, and the position of the parameter in a method signature.
    0 讨论(0)
提交回复
热议问题