diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index a3701a56a92..3d389c9a84c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -760,17 +760,10 @@ internal string GetUsingSet(Language language) /// /// Perform common error checks. /// Populate source code. + /// We only keep the code for backward compatibility. /// protected override void EndProcessing() { - // Prevent code compilation in ConstrainedLanguage mode - if (SessionState.LanguageMode == PSLanguageMode.ConstrainedLanguage) - { - ThrowTerminatingError( - new ErrorRecord( - new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null)); - } - // Generate an error if they've specified an output // assembly type without an output assembly if (String.IsNullOrEmpty(outputAssembly) && outputTypeSpecified) @@ -787,6 +780,37 @@ protected override void EndProcessing() ThrowTerminatingError(errorRecord); return; } + + PopulateSource(); + } + + // We only keep the code for backward compatibility. + internal void PopulateSource() + { + // Prevent code compilation in ConstrainedLanguage mode + if (SessionState.LanguageMode == PSLanguageMode.ConstrainedLanguage) + { + ThrowTerminatingError( + new ErrorRecord( + new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null)); + } + + // Load the source if they want to load from a file + if (String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase) || + String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase) + ) + { + sourceCode = ""; + foreach (string file in paths) + { + sourceCode += System.IO.File.ReadAllText(file) + "\n"; + } + } + + if (String.Equals(ParameterSetName, "FromMember", StringComparison.OrdinalIgnoreCase)) + { + sourceCode = GenerateTypeSource(typeNamespace, Name, sourceCode, language); + } } internal void HandleCompilerErrors(AddTypeCompilerError[] compilerErrors) @@ -891,7 +915,30 @@ public sealed class AddTypeCommand : AddTypeCommandBase /// protected override void EndProcessing() { - base.EndProcessing(); + // Prevent code compilation in ConstrainedLanguage mode + if (SessionState.LanguageMode == PSLanguageMode.ConstrainedLanguage) + { + ThrowTerminatingError( + new ErrorRecord( + new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null)); + } + + // Generate an error if they've specified an output + // assembly type without an output assembly + if (String.IsNullOrEmpty(outputAssembly) && outputTypeSpecified) + { + ErrorRecord errorRecord = new ErrorRecord( + new Exception( + String.Format( + CultureInfo.CurrentCulture, + AddTypeStrings.OutputTypeRequiresOutputAssembly)), + "OUTPUTTYPE_REQUIRES_ASSEMBLY", + ErrorCategory.InvalidArgument, + outputType); + + ThrowTerminatingError(errorRecord); + return; + } if (loadAssembly) {