Author Windows Runtime components with C#/WinRT
Note
Authoring Windows Runtime components with C#/WinRT is supported on .NET 6 and later.
The C#/WinRT NuGet package provides support for authoring your own Windows Runtime types and components in C#, and consuming them from any Windows Runtime-compatible language such as C++/WinRT or Rust. C#/WinRT authoring and hosting support requires .NET 6 and Visual Studio 2022, and it is intended to support desktop application scenarios including the Windows App SDK, and WinUI3.
For a walkthrough showing how to author a Windows Runtime component with .NET 6, and how to consume it from a C++/WinRT console application, see Walkthrough: Create a C#/WinRT component and consume it from C++/WinRT.
For more details, and to search for or file any issues, refer to the C#/WinRT Github repo and Authoring C#/WinRT Components.
Declaring types in Windows Runtime components
While authoring your Windows Runtime component, follow the guidelines and type restrictions outlined in the existing UWP documentation about Windows Runtime components (see Windows Runtime components with C# and Visual Basic). The component can for the most part be implemented like any other C# library. However, there are restrictions on the public types in the component that will be exposed to the Windows Runtime and declared in the generated .winmd
for others to consume.
Externally, you can expose only Windows Runtime types for parameters and return values. You can use built-in C# types as part of the public surface of the component as long as there is a mapping from the .NET type to WinRT (see .NET mappings of WinRT types in C#/WinRT), and they will appear to users of the component as the corresponding Windows Runtime types. Windows Runtime types from other Windows Runtime components and the Windows SDK can also be used as part of the public implementation of the component, such as in
parameters, return types, and class inheritance.
Note
There are some Windows Runtime types that are mapped to .NET types (see .NET mappings of WinRT types in C#/WinRT). These .NET types can be used in the public interface of your Windows Runtime component, and they will appear to users of the component as the corresponding Windows Runtime types.