Change Add-Type -OutputType to not support ConsoleApplication and WindowsApplication (#13440)

This commit is contained in:
Dongbo Wang 2020-08-20 23:30:42 -07:00 committed by GitHub
parent fc4c9cbfd7
commit 5f9d2846f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -551,7 +551,21 @@ namespace Microsoft.PowerShell.Commands
{
ThrowTerminatingError(
new ErrorRecord(
new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null));
new PSNotSupportedException(AddTypeStrings.CannotDefineNewType),
nameof(AddTypeStrings.CannotDefineNewType),
ErrorCategory.PermissionDenied,
targetObject: null));
}
// 'ConsoleApplication' and 'WindowsApplication' types are currently not working in .NET Core
if (OutputType != OutputAssemblyType.Library)
{
ThrowTerminatingError(
new ErrorRecord(
new PSNotSupportedException(AddTypeStrings.AssemblyTypeNotSupported),
nameof(AddTypeStrings.AssemblyTypeNotSupported),
ErrorCategory.NotImplemented,
targetObject: OutputType));
}
}
@ -890,12 +904,9 @@ namespace Microsoft.PowerShell.Commands
{
case OutputAssemblyType.Library:
return OutputKind.DynamicallyLinkedLibrary;
case OutputAssemblyType.ConsoleApplication:
return OutputKind.ConsoleApplication;
case OutputAssemblyType.WindowsApplication:
return OutputKind.WindowsApplication;
default:
throw new ArgumentOutOfRangeException(nameof(outputType));
throw PSTraceSource.NewNotSupportedException();
}
}

View File

@ -165,4 +165,7 @@
<data name="ReferenceAssemblyIgnored" xml:space="preserve">
<value>The specified reference assembly '{0}' is unnecessary and ignored.</value>
</data>
<data name="AssemblyTypeNotSupported" xml:space="preserve">
<value>Both the assembly types 'ConsoleApplication' and 'WindowsApplication' are not currently supported.</value>
</data>
</root>

View File

@ -246,4 +246,12 @@ public class AttributeTest$guid : PSCmdlet
param ($assemblyName, $errorid)
{ Add-Type -AssemblyName $assemblyName } | Should -Throw -ErrorId $errorid
}
It "Throw terminating error when '-OutputType' is '<outputType>'" -TestCases @(
@{ outputType = 'ConsoleApplication' }
@{ outputType = 'WindowsApplication' }
) {
param($outputType)
{ Add-Type -TypeDefinition "Hello" -OutputType $outputType } | Should -Throw -ErrorId 'AssemblyTypeNotSupported,Microsoft.PowerShell.Commands.AddTypeCommand'
}
}