Microsoft.Windows.CsWinRT NuGet Package

July 31, 2026 ยท View on GitHub

Overview

Please visit Microsoft.Windows.CsWinRT for official Microsoft-signed builds of the NuGet package.

The Microsoft.Windows.CsWinRT NuGet package provides WinRT projection support for the C# language. Adding a reference to this package causes C# projection sources to be generated for any Windows metadata referenced by the project. For details on package use, please consult the Microsoft.Windows.CsWinRT github repo's readme.md.

C#/WinRT detects Windows metadata from:

  • Platform winmd files in the SDK
  • NuGet package references containing winmd files
  • Other project references producing winmd files
  • Raw winmd file references

For WinRT types defined in winmd files discovered above, C#/WinRT creates C# projection source files for consuming those WinRT types. These sources can then be compiled into a projection assembly for client projects to reference, or compiled directly into an app.

Details

C#/WinRT sets the following C# compiler properties:

PropertyValueDescription
AllowUnsafeBlockstruePermits compilation of generated unsafe C# blocks

Customizing

C#/WinRT behavior can be customized with these project properties:

PropertyValue(s)Description
CsWinRTEnabled*true | falseEnables projection-related targets, can be set to false to customize build behavior
CsWinRTMessageImportancelow | *normal | highSets the importance of C#/WinRT build messages (see below)
CsWinRTInputs*@(ReferencePath)Specifies WinMD files (beyond the Windows SDK) to read metadata from
CsWinRTExcludes"Windows;Microsoft"Specifies types or namespaces to exclude from projection output
CsWinRTExcludesPrivate"Windows;Microsoft"Specifies types or namespaces to exclude from projection output
CsWinRTIncludes""Specifies types or namespaces to include in projection output
CsWinRTIncludesPrivate""Specifies types or namespaces to include in projection output as internal types
CsWinRTFilters""**Specifies the -includes and -excludes to include in projection output
CsWinRTPrivateFilters""**Specifies the -includes and -excludes to include in projection output as internal types
CsWinRTParams""***Custom cswinrt.exe command-line parameters, replacing default settings below
CsWinRTComponenttrue | *falseSpecifies whether to generate a component (producing) projection from project sources
CsWinRTEnableLoggingtrue | *falseGenerates a log.txt file to help with diagnosing issues with generating the metadata file and sources for a C#/WinRT authoring component
CsWinRTWindowsMetadata<path> | "local" | "sdk" | *$(WindowsSDKVersion)Specifies the source for Windows metadata
CsWinRTGenerateProjection*true | falseIndicates whether to generate and compile projection sources (true), or only to compile them (false)
CsWinRTPrivateProjectiontrue | *falseIndicates if a projection based on CsWinRTIncludesPrivate and related 'private' properties should be generated as internal
CsWinRTGeneratedFilesDir*"$(IntermediateOutputPath)\Generated Files"Specifies the location for generated project source files
CsWinRTIIDOptimizerOptOuttrue | *falseDetermines whether to run the IIDOptimizer on the projection assembly
CsWinRTRcwFactoryFallbackGeneratorForceOptIntrue | *falseForces the RCW factory fallback generator to be enabled (it only runs on .exe projects by default)
CsWinRTRcwFactoryFallbackGeneratorForceOptOuttrue | *falseForces the RCW factory fallback generator to be disabled (overrides "ForceOptIn" as well)
CsWinRTMergeReferencedActivationFactoriestrue | *falseMakes the native DllGetActivationFactory exported function for AOT scenarios also forward the activation call to all referenced WinRT components, allowing them to all be merged into a single executable or shared library
CsWinRTEnableManifestFreeActivation*true | falseEnables the manifest-free WinRT activation path as fallback when RoGetActivationFactory fails to resolve a WinRT type
CsWinRTManifestFreeActivationReportOriginalExceptiontrue | *falseIf 'CsWinRTEnableManifestFreeActivation' is set and activating a type fails, ensures the original exception is thrown, rather than 'NotSupportedException'
CsWinRTUseEnvironmentalTools*true | falseMakes the invocation of MSVC to produce a stub .exe (for Native AOT scenarios) rely on environmental tools, rather than looking up the MSVC install path. This is enabled by default when running in a Visual Studio Developer Command Prompt (or PowerShell), and false otherwise.
CsWinRTAotOptimizerEnabledAuto | OptIn | *true | falseEnables the C#/WinRT AOT optimizer. WinRT types can opt-in to AOT optimization by adding the [GeneratedWinRTExposedType] attribute. More details can be found here
CsWinRTAotWarningLevel0 | *1 | 2 | 3Specifies the warning level used by the code fixer for trimming and AOT compat (0 - no warnings, 1 - warnings for scenarios involving non built-in types, 2 - warnings for all scenarios, 3 - for more extensive warnings for trim/AOT, including noisy ones)
*Default value

**If CsWinRTFilters is not defined, the following effective value is used:

  • -exclude $(CsWinRTExcludes)
  • -include $(CsWinRTIncludes)

***If CsWinRTParam is not defined, the following effective value is used:

  • -target $(TargetFramework)
  • -input $(CsWinRTWindowsMetadata)
  • -input @(CsWinRTInputs)
  • -output $(CsWinRTGeneratedFilesDir)
  • $(CsWinRTFilters)

Advanced size saving options

C#/WinRT supports several feature switches to opt-out of some features available by default, like projection of built-in .NET types. These features are often required by applications, but might be unnecessary in scenarios such as minimal WinRT components authoring. In those cases, it's possible to use these properties to opt-out of any of these features, which can provide binary size savings and performance improvements.

PropertyValue(s)Description
CsWinRTEnableDynamicObjectsSupport*true | falseEnables support for marshalling WinRT types to dynamic objects (ie. ExpandoObject). Setting this to false allows all of System.Linq.Expressions to be trimmed out.
CsWinRTUseExceptionResourceKeystrue | *falseUses minimal, non-localized exception messages to save binary size. Setting this to true allows stripping all of the localization .resw files that are bundled as embedded resources into C#/WinRT.
CsWinRTEnableDefaultCustomTypeMappings*true | falseEnables all available custom type mappings of built-in .NET types by default (eg. INotifyPropertyChanged). Setting this to false allows trimming all code requires to support those features. If you only want to opt-in into a specific subset of built-in type mappings, you can enable just the ones you need via the WinRT.Projections.Register*Mapping() methods. Note that some type mappings will also rely on other configurable features in order to work. For instance, the type mappings for nullable primitive types will not work correctly if the CsWinRTEnableIReferenceSupport feature switch is set to false.
CsWinRTEnableICustomPropertyProviderSupport*true | falseEnables marshalling of ICustomPropertyProvider objects. Setting this to false allows trimming all supporting code for this interface, and will make it not available on marshalled CCW types.
CsWinRTEnableIReferenceSupport*true | falseEnables support and marshalling of IReference<T>, IReferenceArray<T> and IPropertyValue objects. Setting this to false allows trimming all supporting code for all three interfaces, and will make all of them not available on marshalled CCW types.
CsWinRTEnableIDynamicInterfaceCastableSupport*true | falseEnables support for the IDynamicInterfaceCastable infrastructure for RCW types. Setting this to false allows trimming of all related code, and will disallow casting RCW types to interface types they don't directly implement in metadata.
CsWinRTLoadComponentsInDefaultALCtrue | *falseSetting this to true configures WinRT.Host.dll to load CsWinRT components in the default Assembly Load Context (ALC). Note you should ensure components and dependencies loaded are compatible and will not conflict in any way to avoid potential issues. By default, when set to false, they load in their own ALC.
*Default value

Windows Metadata

Windows Metadata is required for all C#/WinRT projections, and can be supplied by:

  • A package reference, such as to Microsoft.Windows.SDK.Contracts
  • An explicit value, supplied by $(CsWinRTWindowsMetadata)
  • The default value, assigned from $(WindowsSDKVersion)

If a Windows SDK is installed, $(WindowsSDKVersion) is defined when building from a VS command prompt.

If no Windows SDK is registered on the machine (for example on clean CI agents, containers, or SDK-less developer machines), the default $(WindowsSDKVersion)/$(TargetPlatformVersion) value is a bare SDK version number that cswinrt.exe can only resolve through the registry, so the build would otherwise fail with "Could not find the Windows SDK in the registry". In that case, for modern .NET TFMs (e.g. net*-windows10.0.x), C#/WinRT automatically falls back to the Windows metadata (.winmd) files shipped in the Microsoft.Windows.SDK.NET.Ref projection ref pack that the .NET SDK restores (its \winmd folder, versioned to $(TargetPlatformVersion)). This fallback only applies when no Windows SDK is registered and $(CsWinRTWindowsMetadata) is still the bare-version default; an explicit $(CsWinRTWindowsMetadata) value (a path, or local/sdk) and registered-SDK builds are unaffected.

Consuming and Producing

The C#/WinRT package can be used both to consume WinRT types, and to produce them (via CsWinRTComponent). It is also possible to combine these settings and do both. For example, a developer might want to produce a library that's implemented in terms of another WinRT runtime class (consuming it).

Troubleshooting

The MSBuild verbosity level maps to MSBuild message importance as follows:

VerbosityImportance
q[uiet]n/a
m[inimal]*high
n[ormal]normal+
d[etailed], diag[nostic]low+
*"high" also enables the cswinrt.exe -verbosity switch

For example, if the verbosity is set to minimal, then only messages with high importance are generated. However, if the verbosity is set to diagnostic, then all messages are generated.

The default importance of C#/WinRT build messages is 'normal', but this can be overridden with the CsWinRTMessageImportance property to enable throttling of C#/WinRT messages independent of the overall verbosity level.

Example:

msbuild project.vcxproj /verbosity:minimal /property:CsWinRTMessageImportance=high ...

For more complex analysis of build errors, the MSBuild Binary and Structured Log Viewer is highly recommended.