diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index f67cdfc4..270485a1 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -299,7 +299,7 @@ internal Generator MainGenerator private bool WideCharOnly => this.options.WideCharOnly; - private string Namespace => this.MetadataIndex.CommonNamespace; + private string Namespace => this.options.Namespace ?? this.MetadataIndex.CommonNamespace; private SyntaxKind Visibility => this.options.Public ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword; @@ -817,7 +817,7 @@ nsContents.Key is object usingDirectives.Add(UsingDirective(ParseName(GlobalNamespacePrefix + "System.Runtime.Versioning"))); } - usingDirectives.Add(UsingDirective(NameEquals(GlobalWinmdRootNamespaceAlias), ParseName(GlobalNamespacePrefix + this.MetadataIndex.CommonNamespace))); + usingDirectives.Add(UsingDirective(NameEquals(GlobalWinmdRootNamespaceAlias), ParseName(GlobalNamespacePrefix + this.Namespace))); var normalizedResults = new Dictionary(StringComparer.OrdinalIgnoreCase); results.AsParallel().WithCancellation(cancellationToken).ForAll(kv => diff --git a/src/Microsoft.Windows.CsWin32/GeneratorOptions.cs b/src/Microsoft.Windows.CsWin32/GeneratorOptions.cs index a060e415..35e99bda 100644 --- a/src/Microsoft.Windows.CsWin32/GeneratorOptions.cs +++ b/src/Microsoft.Windows.CsWin32/GeneratorOptions.cs @@ -19,6 +19,14 @@ public record GeneratorOptions /// The default value is "PInvoke". public string ClassName { get; init; } = "PInvoke"; + /// + /// Gets the root namespace to use for the generated code. This is used to determine the namespace of the generated types. + /// + /// + /// The default namespace is the namespace of the metadata assembly that the generator generates interop code for, e.g. "Windows.Win32". + /// + public string? Namespace { get; init; } + /// /// Gets a value indicating whether to emit a single source file as opposed to types spread across many files. /// diff --git a/src/Microsoft.Windows.CsWin32/settings.schema.json b/src/Microsoft.Windows.CsWin32/settings.schema.json index 8ef51b15..304e8458 100644 --- a/src/Microsoft.Windows.CsWin32/settings.schema.json +++ b/src/Microsoft.Windows.CsWin32/settings.schema.json @@ -78,6 +78,12 @@ "description": "A value indicating whether to expose the generated APIs publicly (as opposed to internally).", "type": "boolean", "default": false + }, + "namespace": { + "description": "The namespace under which all p/invoke methods and constants are generated.", + "type": "string", + "default": null, + "pattern": "^\\w+(?:\\.\\w+)*$" } } }