Razor files contain a C # design buffer with generated C # code (including those parts that you donβt write yourself). This buffer has full Roslyn services and exactly what you are looking for.
You need to go through the TextView BufferGraph and find the CSharp buffer; you can get its Document and semantic model.
If you start with the cursor location, you just need to map this location to the CSharp buffer.
Note that for a TextView, it is acceptable to contain multiple CSharp buffers. (although the Razor editor will never do this)
If you are not working in TextView, you need to do it all yourself; you need to run the Razor source through the Razor compiler to get the generated C # source, and then compile it with Roslyn to get a semantic model.
SLaks source share