C# How to Write a Source Generator Part 1/5: Planning

Kafka Wanna Fly
3 min readDec 15, 2022

--

Source Generator has came with C# 9 and .NET 5 since 2020. A Source Generator is basically the code that writes code for you. This will bring some benefits to your .NET project for example: reduce boiler-plate code, improve performance by replacing reflection, etc.

Figure of a Lego Factory
Artwork of MAHMOUD ELSAEED (https://www.behance.net/autrums19ef6)

Short Introduction for Greeting the Readers

How it works? In short, as developer writes the code, the compiler reads and passes it to the Source Generator (SG). Then, SG analyzes the code and creates new code. Done!

This process happens over and over again whenever your source code’s modified so we have to consider some aspects that impact performance and user experience later on.

We Will Write A Simple but Useful Generator

Compared to others famous frameworks, frameworks from .NET eco-system often require dev to write more code to archive the same thing. In this case, we refer to .NET MAUI (Multi-platform App UI). For those who don’t know, MAUI is a framework for building multi-platform applications running on Android, iOS, Windows and Mac. For the counterparts, they would be React Native and Flutter.

Here is what you have to write to create a UI component in MAUI:

namespace MyMauiApp.Controls;

// This is a TextInput with 2 properties
// You gonna need a least this amount of code
public class TextInput : ContentView
{
public string Text
{
get => (string)GetValue(TextInput.TextProperty);
set => SetValue(TextInput.TextProperty, value);
}

public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text), typeof(string), typeof(TextInput), string.Empty
);

public string PlaceHolder
{
get => (string)GetValue(TextInput.PlaceHolderProperty);
set => SetValue(TextInput.PlaceHolderProperty, value);
}

public static readonly BindableProperty PlaceHolderProperty = BindableProperty.Create(
nameof(PlaceHolder), typeof(string), typeof(TextInput), string.Empty
);


public TextInput()
{
// Implement your logic
}
}

There’re many repeatable parts in this code and we will solve this problem by using a source generator.

At the end of our journey, the code above will be improved and look like this:

using BindableProps;

namespace MyMauiApp.Controls;

// 'partial' keyword tells the compiler that your class might have many pieces, written in many places
// The compiler would go around and look for others 'partial class TextInput'
// Then, combine all of them into a single class
public partial class TextInput : ContentView
{
[BindableProp]
string text;

[BindableProp]
string placeHolder;


public TextInput()
{
// This piece is same as above
}
}

In conclusion, we will write a Source Generator to help reducing amount of code dev would have to write whenever creating a UI component in .NET MAUI.

The job has been done since months ago and I’m telling you the story from the start.

--

--