C # Convert function source code to string

In C #, is there a way to convert the source code of a function to a string? It seems like this can be done in JavaScript. I need this because I am writing some documentation for an Asp.Net control and want to show the source code next to the result without copying and pasting it.

+4
source share
5 answers

The short answer is no.

Long answer: you can generate something at compile time using MSBuild tasks, T4 templates or manually parse things or use Roslyn to analyze it.

If you need source code at runtime, you will definitely need to include the cs files in the deployed material, because there is no stable way to return the code. (However, you can try decompiling at runtime, but it will look ugly and not very accurate.)

+4
source

Cheating Method:

include the .cs file in your project and read from it.

0
source

It looks like you want the function to execute a task at runtime at runtime in order to write the source code in a text file. This is not possible because the function is executed as an ASSEMBLY (or binary) when it is at run time.

However, you could read this copy of your source code and then write it somewhere else.

0
source

You can use one of the following tools:

0
source

You can run the C # compiler as a separate process (it is part of the .NET Framework and is available for it for free - synchronize with it) and load the generated assembly or emit IL-code using the appropriate methods (you need to create the β€œreal” code first IL will prepare such small templates for you first, just write a C # class and check what the similar IL code looks like). Make sure you have the appropriate permissions to do this.

An example of using Emit here, for example (I wrote this a while ago): http://bitcare.codeplex.com/SourceControl/changeset/view/21192#366441

0
source

Source: https://habr.com/ru/post/1445106/


All Articles