Property-of aka infoof — via Rosyln?

大城市里の小女人 提交于 2019-12-10 20:06:35

问题


A few years ago Eric Lippert blogged about a hypothetical infoof operator that would allow you to get method/property information at compile time i stead of at runtime. Basically saying that it's a nice pony, but no unicorn....

I read a bit about Rosyln's ability to rewrite your code at compile time, how difficult would it be to implement infoof using Roslyn?

EDIT: I thought this might be a way to get everything Eric said you they were thinking about, but I should have broken down the features a bit in case it wasn't.

  1. Get a property name.
  2. Get a property type.
  3. Get property accessors.
  4. Get a methods name.
  5. Get everything he meant by infoof...

In a refactoring-safe way, that gets verified at compile time. For me personally, 1 covers about 90% of what I would actually use (the new caller info attribute for getting a callers name can actually cover about 60%, but of course if there was a way that worked both inside and outside of the method, that would be preferable).


回答1:


You can easily use Roslyn to rewrite one valid C# code into another valid C# code. But it's not meant for adding extensions to the language.

Roslyn does parse invalid code too (it has to, if you want to use it for IntelliSense on an incomplete line), but since it's invalid, there is no guarantee it will do it correctly (because it's not clear what exactly does “correctly” mean). Also, I think the structure that it will use for infoof won't be very convenient for you.

To summarize: this is not what Roslyn is meant to be used for and doing it is probably a bad idea. But I think it's not impossible to do this using Roslyn.



来源:https://stackoverflow.com/questions/11256309/property-of-aka-infoof-via-rosyln

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!