Skip to content

Commit

Permalink
Merge pull request PowerShell#1512 from PowerShell/vors/add-type
Browse files Browse the repository at this point in the history
Add-Type: add mscorlib.dll to the list of default references
  • Loading branch information
vors authored Jul 27, 2016
2 parents 3ca8272 + d8a040c commit 5402ba0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
10 changes: 9 additions & 1 deletion build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ function Start-PSBuild {
if ($Clean)
{
log "Cleaning your working directory. You can also do it with 'git clean -fdX'"
git clean -fdX
Push-Location $PSScriptRoot
try
{
git clean -fdX
}
finally
{
Pop-Location
}
}

# save Git description to file for PowerShell to include in PSVersionTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,24 +1059,37 @@ private void CheckTypesForDuplicates(Assembly assembly)
private static PortableExecutableReference ObjectImplementationAssemblyReference =
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);

private static PortableExecutableReference MscorlibAssemblyReference =
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location);

// This assembly should be System.Runtime.dll
private static PortableExecutableReference ObjectDeclaredAssemblyReference =
private static PortableExecutableReference SystemRuntimeAssemblyReference =
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.
// 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 always automatically added to ReferencedAssemblies.
private static PortableExecutableReference[] autoReferencedAssemblies = new PortableExecutableReference[]
{
ObjectImplementationAssemblyReference,
ObjectDeclaredAssemblyReference,
MscorlibAssemblyReference,
SystemRuntimeAssemblyReference,
SecureStringAssemblyReference,
ObjectImplementationAssemblyReference
};

// These assemlbies are used, when ReferencedAssemblies parameter is not specified.
private static PortableExecutableReference[] defaultAssemblies = new PortableExecutableReference[]
{
MscorlibAssemblyReference,
SystemRuntimeAssemblyReference,
SecureStringAssemblyReference,
ObjectImplementationAssemblyReference,
MetadataReference.CreateFromFile(typeof(PSObject).GetTypeInfo().Assembly.Location)
};

Expand Down Expand Up @@ -1218,9 +1231,8 @@ private void CompileSourceToAssembly(string source)
if (referencedAssembliesSpecified)
{
var tempReferences = ReferencedAssemblies.Select(a => MetadataReference.CreateFromFile(ResolveReferencedAssembly(a))).ToList();
tempReferences.Add(ObjectImplementationAssemblyReference);
tempReferences.Add(ObjectDeclaredAssemblyReference);
tempReferences.Add(SecureStringAssemblyReference);
tempReferences.AddRange(autoReferencedAssemblies);

references = tempReferences.ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
$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
}

It "Can load TPA assembly System.Runtime.Serialization.Primitives.dll" {
Add-Type -AssemblyName 'System.Runtime.Serialization.Primitives' -PassThru | Should Not Be $null
}
}

0 comments on commit 5402ba0

Please sign in to comment.