4

In C# is there a way to convert a function's source code to a string? Sort of like how it can be done in JavaScript. I need this because I'm writing some documentation for an Asp.Net control and want to show the original source code next to the result, without copy/pasting it.

1
  • 5
    sounds like a reflection problem. I personally would just read the source file instead Commented Nov 9, 2012 at 17:06

5 Answers 5

5

Short answer: no.

Long answer: you can generate something at compile time with MSBuild tasks, T4 templates, or manually parsing stuff, or using Roslyn to parse it for you.

If you need the source code at runtime though, you'll surely need to include the cs files in the deployed stuff, because there is no stable way of getting the code back. (You could try runtime decompilation though, but that will look "ugly" and not very precise.)

Sign up to request clarification or add additional context in comments.

Comments

0

The cheating way:

include the .cs file in your project, and read from that.

5 Comments

This wouldn't be trivial though. Each function would need to use reflection to access it's own type, you'd need to find the definition of that type in the .cs files (or definitions, in the case of partial classes) and then find the appropriate member within that definition. That's all possible, but it would take a lot of work. Among other things, you'd basically need to make your own C# parser, if you wanted to handle the general case.
@Servy: you don't need to write your own C# parser. There is Roslyn.
@TDaver I thought that was still in beta? Has it officially released?
@Servy: no. But it's in CTP3, and by now it supports almost every c# (and VB) construct...
@TDaver As a rule I avoid providing answers that rely on tools in beta, without asking first; so many people ask questions they're using in commercial applications in which using a tool in beta just isn't feasible, even if it appears to work fine.
0

Sounds like you want the function to do a task while it is in run-time being executed, to write (its) source code in a text file. That is not possible since the function runs as ASSEMBLY (or binary) once it is in run time.

You could however have the function read a copy of its source code from somewhere then write it somewhere else

Comments

0

You can use one of these tools:

Comments

0

You can run C# compiler as a separate process (it's part of .Net Framework and is available for free - do the synchronization with it then) and load generated assembly or emit IL code using proper methods (you have to generate "real" IL code first to prepare such small templates for you first-just write c# class and inspect how generated IL code looks like). Check if yo have proper permissions to do it first.

Sample Emit usage is here for instance (I wrote it some time ago): http://bitcare.codeplex.com/SourceControl/changeset/view/21192#366441

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.