Home

Awesome

NSourceGenerators

NSourceGenerators.CodeToString

Nuget

Roslyn source generator that turns c# source code decorated with [CodeToString] atribute into a string literal.

Demo

A simple console app, that writes its own code to console output:

Input

using NSourceGenerators;

namespace CodeToString.Sample
{
    [CodeToString]
    partial class Program
    {
        [CodeToString("MainKey")]       
        static void Main(string[] args)
        {
            var programCode = CodeToStringRepo.GetText("CodeToString.Sample.Program");
            Console.WriteLine(programCode);

            var mainCode = CodeToStringRepo.GetText("MainKey");
            Console.WriteLine(mainCode);
        }        
    }
}

Output

    partial class Program
    {
        [CodeToString("MainKey")]
        static void Main(string[] args)
        {
            var programCode = CodeToStringRepo.GetText("CodeToString.Sample.Program");
            Console.WriteLine(programCode);

            var mainCode = CodeToStringRepo.GetText("MainKey");
            Console.WriteLine(mainCode);
        }
    }
        static void Main(string[] args)
        {
            var programCode = CodeToStringRepo.GetText("CodeToString.Sample.Program");
            Console.WriteLine(programCode);

            var mainCode = CodeToStringRepo.GetText("MainKey");
            Console.WriteLine(mainCode);
        }