<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: xafero</title>
    <description>The latest articles on Forem by xafero (@xafero).</description>
    <link>https://forem.com/xafero</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1408956%2F588f5a12-1834-4914-89f8-190b8fb4404a.png</url>
      <title>Forem: xafero</title>
      <link>https://forem.com/xafero</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xafero"/>
    <language>en</language>
    <item>
      <title>Generating C# code programmatically</title>
      <dc:creator>xafero</dc:creator>
      <pubDate>Sat, 06 Apr 2024 01:04:46 +0000</pubDate>
      <link>https://forem.com/xafero/generating-c-code-programmatically-5981</link>
      <guid>https://forem.com/xafero/generating-c-code-programmatically-5981</guid>
      <description>&lt;p&gt;Recently, while creating some experimental C# source code generators (&lt;a href="https://github.com/xafero/csharp-generators"&gt;xafero/csharp-generators&lt;/a&gt;), I was just concatenating strings together. Like you do, you know, if things have to go very quickly. If you have a simple use case, use a &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated#interpolated-raw-string-literals"&gt;formatted multi-line string &lt;/a&gt; or some template library like &lt;a href="https://github.com/scriban/scriban"&gt;scriban&lt;/a&gt;. But I searched for a way to generate more and more complicated logic easily - like for example, adding raw SQL handler methods to my pre-generated DBSet-like classes for my ADO.NET experiment. You could now say: Use &lt;a href="https://github.com/dotnet/roslyn"&gt;Roslyn&lt;/a&gt; and that's really fine if you look everything up in a website like &lt;a href="https://sharplab.io/"&gt;SharpLab&lt;/a&gt;, which shows immediately the syntax tree of our C# code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Instead, I propose the following mutable model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var unit = new CUnit { Usings = { "System" }, Members = { 
     new CClass { Name = "TestClass", Members = {
       new CMethod { Name = "Main", IsStatic = true,
           Params = { new CParam { Type = "string[]", 
               Name = "args" } }, Body = new() { 
               Statements = { "Console.WriteLine(args.Length)" } 
               } } } } } };
Console.WriteLine(unit.ToText());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which then produces code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;

public class TestClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine(args.Length);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why the heck?
&lt;/h2&gt;

&lt;p&gt;The meta code model is mutable and allows for much easier manipulation of to-be-generated C# code than the Roslyn compiler provides. You can come back and modify parts of it freely, while also not having to remember if you put a semicolon at this position or not. The abstraction layer handles those minor details of the syntax tree instead! I hope to remove all unnecessary string allocations from my code and improve those C# source code generators over time. &lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;p&gt;If you'd like to check it out on NuGet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.nuget.org/packages/Coaster"&gt;Coaster&lt;/a&gt; for generating C# code more easily&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.nuget.org/packages/Cscg.Constants"&gt;Cscg.Constants&lt;/a&gt; for constants by build scripts&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.nuget.org/packages/Cscg.StubCreator"&gt;Cscg.StubCreator&lt;/a&gt; for creating stubs out of C# XML doc&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.nuget.org/packages/Cscg.Compactor"&gt;Cscg.Compactor&lt;/a&gt; for serializing POCOs to CBOR &amp;amp; XML&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.nuget.org/packages/Cscg.AdoNet"&gt;Cscg.AdoNet&lt;/a&gt; for EFCore-like pregenerated SQL wrappers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally, please click &lt;a href="https://github.com/sponsors/xafero"&gt;here&lt;/a&gt; if anything takes your fancy.&lt;/p&gt;

</description>
      <category>charp</category>
      <category>codegen</category>
      <category>roslyn</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
