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.
This commit is contained in:
Sergei Vorobev
2016-07-26 17:45:49 -07:00
parent 3ca8272d41
commit 3fd885ef2d
2 changed files with 35 additions and 8 deletions
@@ -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();
}
@@ -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
}
}