Use-cases for reflection

后端 未结 9 2119
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 20:34

Recently I was talking to a co-worker about C++ and lamented that there was no way to take a string with the name of a class field and extract the field with that name; in o

相关标签:
9条回答
  • 2020-12-08 20:56

    I can list following usage for reflection:

    • Late binding
    • Security (introspect code for security reasons)
    • Code analysis
    • Dynamic typing (duck typing is not possible without reflection)
    • Metaprogramming

    Some real-world usages of reflection from my personal experience:

    • Developed plugin system based on reflection
    • Used aspect-oriented programming model
    • Performed static code analysis
    • Used various Dependency Injection frameworks
    • ...

    Reflection is good thing :)

    0 讨论(0)
  • 2020-12-08 20:59

    I'm in a situation now where I have a stream of XML coming in over the wire and I need to instantiate an Entity object that will populate itself from elements in the stream. It's easier to use reflection to figure out which Entity object can handle which XML element than to write a gigantic, maintenance-nightmare conditional statement. There's clearly a dependency between the XML schema and how I structure and name my objects, but I control both so it's not a big problem.

    0 讨论(0)
  • 2020-12-08 21:01

    Writing dispatchers. Twisted uses python's reflective capabilities to dispatch XML-RPC and SOAP calls. RMI uses Java's reflection api for dispatch.

    Command line parsing. Building up a config object based on the command line parameters that are passed in.

    When writing unit tests, it can be helpful to use reflection, though mostly I've used this to bypass access modifiers (Java).

    0 讨论(0)
  • 2020-12-08 21:02

    I've used reflection to get current method information for exceptions, logging, etc.

    string src = MethodInfo.GetCurrentMethod().ToString();
    string msg = "Big Mistake";
    Exception newEx = new Exception(msg, ex);
    newEx.Source = src;
    

    instead of

    string src = "MyMethod";
    string msg = "Big MistakeA";
    Exception newEx = new Exception(msg, ex);
    newEx.Source = src;
    

    It's just easier for copy/paste inheritance and code generation.

    0 讨论(0)
  • 2020-12-08 21:06

    I generally use reflection for debugging. Reflection can more easily and more accurately display the objects within the system than an assortment of print statements. In many languages that have first-class functions, you can even invoke the functions of the object without writing special code.

    There is, however, a way to do what you want(ed). Use a hashtable. Store the fields keyed against the field name.

    If you really wanted to, you could then create standard Get/Set functions, or create macros that do it on the fly. #define GetX() Get("X") sort of thing.

    You could even implement your own imperfect reflection that way.

    For the advanced user, if you can compile the code, it may be possible to enable debug output generation and use that to perform reflection.

    0 讨论(0)
  • 2020-12-08 21:12

    I've used reflection in C# when there was some internal or private method in the framework or a third party library that I wanted to access.

    (Disclaimer: It's not necessarily a best-practice because private and internal methods may be changed in later versions. But it worked for what I needed.)

    0 讨论(0)
提交回复
热议问题