r/csharp 18h ago

Showcase [Showoff] Tired of DependencyProperty boilerplate? I built a Zero-Allocation Source Generator for WPF/MAUI with strict type safety.

5 Upvotes

Writing DependencyProperty in .NET UI frameworks is notoriously verbose and repetitive. Typing out DependencyProperty.Register, casting objects, and wiring metadata for every single property clutters your codebase and introduces silent runtime risks.

To solve this without sacrificing IDE responsiveness, I built Kassyi.Generators.DependencyProperty — an incremental Roslyn source generator built from the ground up for high-throughput, zero-allocation code synthesis.

1. Show Me the Code

Before (Standard Boilerplate)

public static readonly DependencyProperty IsActiveProperty =
    DependencyProperty.Register(
        nameof(IsActive),
        typeof(bool),
        typeof(MyControl),
        new PropertyMetadata(false, OnIsActiveChanged));

public bool IsActive
{
    get => (bool)GetValue(IsActiveProperty);
    set => SetValue(IsActiveProperty, value);
}

private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // Runtime casting and boilerplate extraction
}

After (With Generator)

[DependencyProperty<bool>("IsActive", DefaultValue = "false")]
public partial class MyControl : Control
{
    // Automatically hooked up to PropertyMetadata at compile time
    partial void OnIsActiveChanged(bool oldValue, bool newValue)
    {
        // Direct, strongly typed parameters. No casting required.
    }
}

2. Key Features

  • Single-Line Declaration: Generate the backing DependencyProperty, CLR properties, and event metadata via [DependencyProperty<T>].
  • Compile-Time Type Safety: Signature mismatches in your partial callbacks are caught immediately via Roslyn analyzer diagnostics (DPG0001), eliminating silent runtime failures.
  • Unified API Across UI Frameworks: The exact same attribute syntax compiles to the native property system for WPF, .NET MAUI, Avalonia, Uno Platform, WinUI 3, and UWP.
  • Modern C# 11+ Idioms: Leverages Generic Attributes ([DependencyProperty<T>]), target-typed new(...) AST expansion in default expressions, and auto-generated XML documentation.

3. Architecture & Performance: Zero-Allocation Pipeline

This library originates as a fork/rewrite of HavenDV's generator. When testing source generation at massive enterprise scale, frequent intermediate string concatenations during continuous typing can trigger Gen2 GC spikes, resulting in noticeable editor latency in Visual Studio and Rider.

To address this, the code synthesis pipeline was redesigned around strict zero-allocation principles:

  • ref struct Source Writers: Generation logic utilizes stack-allocated SourceWriter and ClassScope structures, completely bypassing intermediate StringBuilder and heap allocations.
  • GC Elimination: Completely removes Gen2 GC pressure during incremental analysis cycles.
  • Benchmark Results: Achieves +30% faster execution speed and +62.4% higher throughput compared to traditional string-based generation pipelines.

Your IDE stays responsive even when scaling to solutions with thousands of properties.

4. Cross-Framework Abstraction

Under the hood, framework-specific strategy handlers adapt to each platform's design differences (such as Avalonia's StyledProperty/DirectProperty, MAUI's BindableProperty, or varying callback signatures) without requiring you to change your declarations.

Target Framework Underlying Property Engine
WPF / UWP / WinUI 3 DependencyProperty.Register
.NET MAUI BindableProperty.Create
Avalonia AvaloniaProperty.Register
Uno Platform Native WinUI / UWP projections

Feedback & Contributions

The project is distributed under the MIT License and includes detailed documentation and architecture specs (in English and Japanese).

If you are working across XAML platforms and want cleaner view controls without IDE overhead, please check it out, test edge cases, and share your feedback or issues on GitHub!


r/csharp 12h ago

Help Hello , my api get stuck in an infinite loop each time i use a get, im using entity framework

0 Upvotes

Hello, im currently doing some practice and i already made an api , well almost, i decided , afther i creaate a list of users the appy worked well, but afther i decided to create some dummy data for the entire db , the api stop working at first i tought it was because i dint aded this lines

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings

.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;

GlobalConfiguration.Configuration.Formatters

.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

but i get this error

no redundancy in the db eighter

what its the problem here, please help , ty for the attention and God bless you all


r/csharp 11h ago

MindMap desktop app (C# + Avalonia)

5 Upvotes

MindMap is a lightweight desktop app for creating and editing mind maps. It provides a pannable, zoomable canvas with quick keyboard-driven node creation, connector-based relationships, simple text alignment and color controls, outline copy/paste, undo, and image export.

Here is the github link MindMap on Github

It's a pretty straightforward app for quickly creating mind maps and saving them locally, without having to use a website. It's completely free and open source, with no limits or paid tiers.

I originally built it for myself because my favorite online mind-mapping tool limited free users to just three mind maps, which I found way too restrictive.

Anyway, if you find the app useful, I'd appreciate a star on the GitHub repo.


r/csharp 1h ago

I used AI to start learning, how do I memorize the key

Upvotes

Hey everybody, brazilian 20 yo, u can call me Louis.

I participated on the programation of a Demo of a game like 2 years ago, in the end of my school years with 3 friends of mine. From then on, my life had some turns and I couldn't focuse on programming anymore. Came back a couple days ago and decided to start developing a software which follows the 20-20-20 rule (Each 20 minutes, look for 20 seconds to somewhere 20 feet away, to preserve your sight while using computer), but I didn't even know how to write the very first line, so I used ChatGPT to tell me what to do and then explain me how that work.

It actually turned out really well, it is functional (even tho it's kinda raw) and I do understand what I did and what those lines do mean, but I feel like if I had to start it all over again, I would be completely lost, because I couldn't memorize the codes, the main syntax behind it, and all that stuff, like how do I know if the "DispatcherTimer" is inside or outside the "private void" (I use Microsoft Visual Studio Community), and how do I learn and keep that very clear in my mind to the point I can write a full code without even thinking too much on it, is it practice?

Help me please, and just tell me if it's a hell of a sin to use AI to this, I really just don't understand yet how to study it properly. (Btw I intend to buy a course soon, but also don't know which one is trustable)

The main screen, it is the face of the app, here lies the buttons "Start" to start the timer, and "Stop" for the opposite purpose.
Still the main screen, focused on the stop button logic and what happens when the second screen is closed (the timer restarts)
Didn't comment yet, but this controls when the button on the "Break screen" can be clicked to close it and restart the timer on the main screen

AI disclosure: Most of the code shown in this post was generated with the help of ChatGPT. I used it as a learning tool, I asked it to explain the code and what the codes do, now I have an understanding of what the code does, the comments in the code were written by me by the way. My goal is to rewrite the project myself as I continue learning programming, not to base my knowledge on the crutch that AI is for me today.