Use-cases for reflection

后端 未结 9 2165
隐瞒了意图╮
隐瞒了意图╮ 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 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.

提交回复
热议问题