Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property-of aka infoof -- via Rosyln?

Tags:

.net

roslyn

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).

like image 215
jmoreno Avatar asked Jun 29 '12 05:06

jmoreno


1 Answers

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.

like image 195
svick Avatar answered Nov 03 '22 16:11

svick