Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/NeVeSpl/NSourceGenerators
https://github.com/NeVeSpl/NSourceGenerators
roslyn
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/NeVeSpl/NSourceGenerators
- Owner: NeVeSpl
- License: mit
- Created: 2023-10-19T16:13:09.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-09T11:44:54.000Z (7 months ago)
- Last Synced: 2024-05-22T04:33:51.105Z (7 months ago)
- Topics: roslyn
- Language: C#
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - NSourceGenerators
README
# NSourceGenerators
## NSourceGenerators.CodeToString
[![Nuget](https://img.shields.io/nuget/v/NSourceGenerators.CodeToString?color=%23004880&label=NSourceGenerators.CodeToString)](https://www.nuget.org/packages/NSourceGenerators.CodeToString)
Roslyn source generator that turns c# source code decorated with `[CodeToString]` atribute into a string literal.
- `[CodeToString]` attribute can be placed on: classes, structs and methods
- access to generated string representation is provided through static method: `CodeToStringRepo.GetText("key")`
- generated code uses raw string literals ``` that were introduced in C# 11
- user can provide custom keys, if not provided, full symbol name with namespace is used as a key
### DemoA simple console app, that writes its own code to console output:
Input
```csharp
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);
}```