From 3fd885ef2def0f1dfcb105657c8147e81c79b5ca Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Mon, 25 Jul 2016 18:56:49 -0700 Subject: [PATCH] Add-Type: add mscorlib.dll to the list of default references Fix #1361 On CoreCLR 1.0.0 RTM the layout changed compare to RC2. This change accounts for it. This change also cleans up some left-overs for SecureString ref. --- .../commands/utility/AddType.cs | 31 ++++++++++++++----- .../Add-Type.Tests.ps1 | 12 ++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 58a848aeba..93254717a6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -1063,19 +1063,34 @@ namespace Microsoft.PowerShell.Commands private static PortableExecutableReference ObjectDeclaredAssemblyReference = MetadataReference.CreateFromFile(ClrFacade.GetAssemblies(typeof(object).FullName).First().Location); - // CoreCLR RC2 bits don't have SecureString. We are using a separate assembly with SecureString implementation. + // In CoreCLR 1.0.0 RTM, System.Attribute need to be loaded from mscorlib.dll, + // not from ObjectDeclaredAssemblyReference. + private static PortableExecutableReference AttributeDeclaredAssemblyReference = + MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location); + + // SecureString is defined in a separate assembly. // This fact is an implementation detail and should not require the user to specify one more assembly, // if they want to use SecureString in Add-Type -TypeDefinition. // So this assembly should be in the default assemblies list to provide the best experience. - // - // TODO: This reference should be removed, if we take CoreCLR version that has SecureString implementation. private static PortableExecutableReference SecureStringAssemblyReference = MetadataReference.CreateFromFile(typeof(System.Security.SecureString).GetTypeInfo().Assembly.Location); - private static MetadataReference[] defaultAssemblies = new MetadataReference[] + + // These assemlbies are automatically added to ReferencedAssemblies. + private static PortableExecutableReference[] autoReferencedAssemblies = new PortableExecutableReference[] { ObjectImplementationAssemblyReference, ObjectDeclaredAssemblyReference, + AttributeDeclaredAssemblyReference, + SecureStringAssemblyReference + }; + + // These assemlbies are used, when ReferencedAssemblies parameter is not specified. + private static PortableExecutableReference[] defaultAssemblies = new PortableExecutableReference[] + { + ObjectImplementationAssemblyReference, + ObjectDeclaredAssemblyReference, + AttributeDeclaredAssemblyReference, SecureStringAssemblyReference, MetadataReference.CreateFromFile(typeof(PSObject).GetTypeInfo().Assembly.Location) }; @@ -1218,9 +1233,11 @@ namespace Microsoft.PowerShell.Commands if (referencedAssembliesSpecified) { var tempReferences = ReferencedAssemblies.Select(a => MetadataReference.CreateFromFile(ResolveReferencedAssembly(a))).ToList(); - tempReferences.Add(ObjectImplementationAssemblyReference); - tempReferences.Add(ObjectDeclaredAssemblyReference); - tempReferences.Add(SecureStringAssemblyReference); + foreach (var reference in autoReferencedAssemblies) + { + tempReferences.Add(reference); + } + references = tempReferences.ToArray(); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 index 142e7e0f5e..f6f8d5f3b6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 @@ -1,5 +1,15 @@ +$guid = [Guid]::NewGuid().ToString().Replace("-","") + Describe "Add-Type" { It "Should not throw given a simple class definition" { - { Add-Type -TypeDefinition "public static class foo { }" } | Should Not Throw + { Add-Type -TypeDefinition "public static class foo { }" } | Should Not Throw + } + + It "Can use System.Management.Automation.CmdletAttribute" { + $code = @" +[System.Management.Automation.Cmdlet("Get", "Thing", ConfirmImpact = System.Management.Automation.ConfirmImpact.High, SupportsPaging = true)] +public class AttributeTest$guid {} +"@ + Add-Type -TypeDefinition $code -PassThru | Should Not Be $null } }