Skip to sessions and notes

These session notes are AI-generated and may contain mistakes. Verify important details against the original recording.

CsWinRT 3.0: Modernizing the Windows Projections for .NET

Mano, Sergio Pedri

Session · 25:57 recording · 8 min read

At a glance

  • Ground-up redesign eliminating diamond dependency & version coupling hell
  • Reference Projections: NuGet packages ship ref/ assemblies + metadata/.winmd
  • Consumer app dynamically generates unified WinRT.Projection.dll & WinRT.Interop.dll
  • App developer is in 100% control of projection runtime version
  • Microsoft Store client on Native AOT: 129 MB → 107 MB (-22 MB, >16% reduction)

Executive Summary

At .NET Conf 2026, Mano and Sergio Pedri officially unveiled CsWinRT 3.0, a ground-up re-architecture of the C#/WinRT projection toolchain that bridges modern .NET applications with native Windows Runtime (WinRT) APIs.

Originally introduced during the .NET 5 era, CsWinRT evolved across multiple .NET versions, retrofitting trimming and Native AOT support while preserving strict backwards compatibility. Over time, this backward-compatibility commitment created major developer friction: version coupling and diamond dependency hell across NuGet packages, heavy Roslyn source generator overhead that froze Visual Studio during editing, and awkward code annotations ([DynamicWindowsRuntimeCast], mandatory partial classes, and array allocation workarounds).

CsWinRT 3.0 breaks through these architectural ceilings by introducing:

  1. Reference Projections Architecture: Libraries now ship reference-only assemblies alongside raw WinMD metadata. The consumer application's chosen CsWinRT toolchain dynamically generates unified WinRT.Projection.dll and WinRT.Interop.dll assemblies at publish/build time, putting the app developer in 100% control of the projection version and eliminating package version mismatches.
  2. Massive Binary Size Savings on Native AOT:
    • Microsoft Store Client: Reduced executable footprint from 129 MB down to 107 MB (-22 MB, >16% reduction).
    • Contoso Camera App: Down from 5.6 MB to 5.08 MB (~10% reduction).
  3. IDE Inner-Loop Acceleration: Removed expensive Roslyn source generators from the daily IntelliSense loop. Clean build generator execution dropped from 115 seconds to 9 seconds (13x faster); incremental builds dropped from 24 seconds to 4 seconds (6x faster).
  4. TFM Opt-in via the .1 Suffix: Non-breaking opt-in via Target Framework Monikers (e.g., net11.0-windows10.0.26100.1 vs .0).
  5. Modern C# Array Projections via Spans: WinRT Pass Arrays project as ReadOnlySpan<T>, Fill Arrays project as Span<T>, enabling zero-allocation slicing, stackalloc, and eliminating temporary array allocations.
  6. Native ABI Precision: WinRT Point, Rect, and Size now use native float instead of legacy double, eliminating thousands of implicit conversions.

1. History & Motivation: Why Rebuild CsWinRT?

Title: CsWinRT 3.0: Modernizing the Windows Projections for .NET
Title: CsWinRT 3.0: Modernizing the Windows Projections for .NET

When CsWinRT shipped in .NET 5, neither trimming nor Native AOT existed in modern .NET. As trimming arrived in .NET 6 and Native AOT in .NET 7/8, the team retrofitted compatibility incrementally. However, to avoid breaking existing binary NuGet packages, they could not alter generated ABI structures, assembly forwarding mechanisms, or type mappings.

CsWinRT History and Motivation
CsWinRT History and Motivation

This accumulated significant technical debt:

  • Applications were forced into specific CsWinRT runtime versions by third-party UI controls.
  • Developers had to sprinkle internal attributes ([DynamicWindowsRuntimeCast]) across everyday business logic.
  • Roslyn analyzers evaluated massive projection metadata inside Visual Studio, causing noticeable typing stutter and delayed IntelliSense.

CsWinRT 3.0 resets this baseline by leveraging .NET 11 and C# 14 features natively.


2. Production Performance: Microsoft Store Client Benchmarks

Binary Size: Microsoft Store Client Reduction
Binary Size: Microsoft Store Client Reduction

Sergio shared real-world telemetry from the production Microsoft Store Client app running on Native AOT:

Metric CsWinRT 2.x (.NET 10 AOT) CsWinRT 3.0 (.NET 10/11 AOT) Impact / Speedup
Microsoft Store Executable Size 129 MB 107 MB -22 MB (-16.2% reduction)
Contoso Camera Executable Size 5.6 MB 5.08 MB -0.52 MB (-9.3% reduction)
Store Clean Interop Gen CPU Time 115 seconds 9 seconds 12.8x faster
Store Incremental Build Time 24 seconds 4 seconds 6x faster

Build Time and IntelliSense Wins
Build Time and IntelliSense Wins

Why Build Times Plummeted

In CsWinRT 2.x, a heavy Roslyn Source Generator scanned every syntax tree on every keystroke to determine marshalling requirements. In CsWinRT 3.0:

  • The Roslyn source generator is completely removed from the IDE design-time loop.
  • A standalone, native compiled interop generator runs exclusively when pressing F5 or publishing.
  • Visual Studio remains responsive with instantaneous IntelliSense.

3. How WinRT & Projections Work

What is WinRT & How Projections Work
What is WinRT & How Projections Work

Windows Runtime (WinRT) APIs are described in language-agnostic .winmd (Windows Metadata) files. A language projection parses this metadata and produces types that feel native to that programming language:

// Consuming WinRT via .NET TFM
// csproj: <TargetFramework>net11.0-windows10.0.26100.1</TargetFramework>

using Windows.Devices.Geolocation;

var locator = new Geolocator();
var pos = await locator.GetGeopositionAsync();
Console.WriteLine($"Latitude: {pos.Coordinate.Point.Position.Latitude}");

Developers can also author WinRT components in C# for consumption by C++, Rust, or JavaScript using CsWinRTComponent=true:

Authoring WinRT Components in C#
Authoring WinRT Components in C#

Idiomatic C# Surface: LINQ, Spans, and Pattern Matching
Idiomatic C# Surface: LINQ, Spans, and Pattern Matching

Projections map WinRT types into native C# idioms:

  • Windows.Foundation.IAsyncOperation<T> maps directly to await Task<T>.
  • IVectorView<T> maps to IReadOnlyList<T>, allowing immediate use of LINQ (Where, FirstOrDefault).
  • WinRT property bags accept modern C# Collection Expressions ([ "Title", "Author" ]).

4. Solving the Major Pain Points

Pain Point 1: Version Coupling & Diamond Dependency Hell

Pain Point: Version Coupling in CsWinRT 2.x
Pain Point: Version Coupling in CsWinRT 2.x

In CsWinRT 2.x, NuGet component packages (e.g., UI controls, charting libraries) compiled and packaged concrete projection assemblies. If Package A was compiled with CsWinRT 2.0.8 and Package B with 2.1.0, the consuming application suffered assembly mismatch compilation errors. Even if a bug was fixed in CsWinRT 2.2, developers could not benefit until all upstream package authors recompiled and republished their packages.

Pain Point 2: Trimming & AOT Boilerplate Friction

Pain Point: AOT Friction with Attributes & Partial Classes
Pain Point: AOT Friction with Attributes & Partial Classes

To satisfy trimming and AOT compilers in 2.x, developers were forced to write awkward, unnatural C#:

// CsWinRT 2.x: Unnatural boilerplate
public partial class TagButton : Button // Forced to be partial!
{
    [DynamicWindowsRuntimeCast(typeof(FrameworkElement))] // Required internal attribute!
    public void ProcessTags(object element)
    {
        if (element is FrameworkElement fe)
        {
            // Explicit array cast required to prevent compiler-generated hidden types
            TagHelper.ApplyTags((string[])[ "Tag1", "Tag2" ]);
        }
    }
}

In CsWinRT 3.0, all of this boilerplate is eliminated:

CsWinRT 3.0: Clean, Natural C#
CsWinRT 3.0: Clean, Natural C#

// CsWinRT 3.0: Clean, idiomatic C#
public class TagButton : Button // No partial required
{
    public void ProcessTags(object element)
    {
        if (element is FrameworkElement fe) // Clean pattern match
        {
            TagHelper.ApplyTags([ "Tag1", "Tag2" ]); // Pure collection expression
        }
    }
}

5. The Reference Projections Architecture

Reference Projections Architecture
Reference Projections Architecture

flowchart TD
    subgraph NuGetPackages["NuGet Packages / Component Libraries"]
        PkgA["Package A (ref/PackageA.dll + metadata/PackageA.winmd)"]
        PkgB["Package B (ref/PackageB.dll + metadata/PackageB.winmd)"]
        WinSDK["Windows SDK Targeting Pack (WinMDs)"]
    end

    subgraph AppBuild["Consumer Application Build Pipeline"]
        App["App Project (Chooses CsWinRT 3.0)"]
        Collector["WinMD Metadata Collector"]
        ProjGen["Projection Generator"]
        InteropGen["Native Compiled Interop Generator"]
    end

    subgraph EmittedAssemblies["Unified Emitted Assemblies"]
        WinRTProj["WinRT.Projection.dll (Unified Types)"]
        WinRTInterop["WinRT.Interop.dll (Analyzed App Interop)"]
    end

    PkgA --> Collector
    PkgB --> Collector
    WinSDK --> Collector
    Collector --> ProjGen
    App --> InteropGen
    ProjGen --> WinRTProj
    InteropGen --> WinRTInterop

How Reference Projections Work:

  1. Reference-Only Packages: Component packages only ship API signatures in ref/ assemblies and raw metadata in a metadata/ folder.
  2. App-Driven Compilation: When the application builds, it gathers all referenced .winmd files.
  3. Unified Assemblies: The app’s selected CsWinRT generator emits a single WinRT.Projection.dll (containing all projected types) and a single WinRT.Interop.dll (containing custom marshalling for classes implementing WinRT interfaces).
  4. Zero Diamond Dependencies: Upstream package authors never lock consumers to a specific projection version.

6. The .1 TFM Opt-In & Multi-Targeting

TFM Opt-in via .1 Suffix and Multi-Targeting
TFM Opt-in via .1 Suffix and Multi-Targeting

To avoid breaking existing applications, Microsoft and the NuGet team introduced an explicit revision scheme in the Windows Target Framework Moniker (TFM):

Target Framework Moniker Projection Version Compatibility Target
net11.0-windows10.0.26100.0 CsWinRT 2.x Legacy applications, existing controls
net11.0-windows10.0.26100.1 CsWinRT 3.0 Modern reference projections, zero boilerplate

For component authors who need to support both 2.x and 3.0 consumers, CsWinRT 3.0 provides the #if CSWINRT3_0 preprocessor symbol:

#if CSWINRT3_0
    // Uses .NET standard System.EventHandler<TTarget, TArgs>
    public event System.EventHandler<Timer, TickEventArgs> Tick;
#else
    // Legacy CsWinRT 2.x projection type
    public event Windows.Foundation.TypedEventHandler<Timer, TickEventArgs> Tick;
#endif

7. High-Performance Array Marshalling with Span<T>

Span and ReadOnlySpan Array Marshalling
Span and ReadOnlySpan Array Marshalling

WinRT defines three array semantics:

  1. Pass Arrays (Input only): Callee only reads from the array.
  2. Fill Arrays (Output buffer): Caller passes an allocated buffer; callee populates it.
  3. Receive Arrays (Return array): Callee allocates and returns an array.

Historically, all three mapped to T[], causing severe allocation waste. CsWinRT 3.0 modernizes this mapping:

WinRT Array Type CsWinRT 2.x Mapping CsWinRT 3.0 Mapping Performance Advantage
Pass Array T[] ReadOnlySpan<T> Supports stackalloc, span slicing, zero allocation
Fill Array T[] Span<T> Direct slicing into existing memory buffers
Receive Array T[] T[] Native array return retained

Code Comparison: Temporary Array Allocation vs Direct Span Slice
Code Comparison: Temporary Array Allocation vs Direct Span Slice

// CsWinRT 2.x: Mandatory temporary allocation and copy
byte[] temp = new byte[16];
dataReader.ReadBytes(temp);
Array.Copy(temp, 0, destinationBuffer, offset, 16);

// CsWinRT 3.0: Zero-allocation direct span slice
dataReader.ReadBytes(destinationBuffer.AsSpan(offset, 16));

8. Live Migration Walkthrough (Contoso Camera App)

Live Migration: Contoso Camera in Visual Studio
Live Migration: Contoso Camera in Visual Studio

Mano walked through migrating the Contoso Camera sample app to CsWinRT 3.0:

  1. Update TFM: Changed net10.0-windows10.0.26100.0 to net11.0-windows10.0.26100.1.
  2. Package References: Updated Microsoft.Windows.CsWinRT to 3.0.0-preview and targeting pack to 88-preview.
  3. Remove Legacy Interop Usings: using System.Runtime.InteropServices.WindowsRuntime; is no longer required because extension methods (AsTask, AsStream) now live in Windows.Foundation.
  4. Strip AOT Attributes: Deleted hundreds of [DynamicWindowsRuntimeCast] attributes across the codebase.
  5. Float Coordinates: Updated Point/Rect coordinates to use native float values, matching native DirectX/WinUI math without hidden float-double conversions.

Key Takeaways

Feature CsWinRT 2.x CsWinRT 3.0
Projection Delivery Concrete assemblies in packages Reference projections + on-the-fly merge
Version Ownership Dictated by third-party packages 100% owned by app developer
Store App Size 129 MB 107 MB (-16%)
IntelliSense Build 115s CPU time (Roslyn generator) 9s CPU time (13x faster)
Array Marshalling T[] only ReadOnlySpan<T> and Span<T>
Syntax Overhead partial, custom cast attributes Pure standard C# syntax
Opt-in TFM ...26100.0 ...26100.1