Compile Time Reflection in C#

前端 未结 4 805
鱼传尺愫
鱼传尺愫 2020-12-13 09:55

I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 10:44

    Yet C#/.NET uses them all over the place to represent property/class/method names.

    First off: I disagree. There are certain frameworks (WebForms, e.g.) that use magic strings all over the place, but the base libraries for C# and .NET tend to avoid such things remarkably well.

    Secondly: In many instances where magic strings are used, ReSharper is able to recognize errors. This can help quite a bit.

    Finally: What you're asking for may be possible via the Roslyn Compiler, which promises to provide "Compiling as a service."

提交回复
热议问题