How to access a type defined by Add-Type -TypeDefinition \"...\"
in another Add-Type -TypeDefinition \"...\"
?
In the following code example
In order to reference the first .NET assembly from the second .NET assembly, you need to persist the first .NET assembly to disk. Once you've done this, you can reference the first .NET assembly from the second .NET assembly using the -ReferencedAssemblies
parameter of the Add-Type
cmdlet.
IMPORTANT: If you use two different namespaces in your two different C# code blocks, make sure that you declare your using
statements appropriately in the second code block.
Here is an example, showing you how to:
The first .NET Assembly must be output to disk so that we can reference it. Therefore, we will use the -OutputAssembly
and -OutputType
parameters of the Add-Type
cmdlet to create it.
$Csharp = @"
using System;
using System.Reflection;
namespace Widget {
public class UserCode {
public static int GetValue() {
return 2;
}
}
}
"@
# Define the output path for the new .NET Assembly
$OutputAssembly = '{0}\Widget.dll' -f $env:USERPROFILE;
# Compile the code and output a new .NET assembly
Add-Type -TypeDefinition $Csharp -OutputAssembly $OutputAssembly -OutputType Library;
# Load the .NET Assembly
[System.Reflection.Assembly]::LoadFile($OutputAssembly);
We don't have to worry about compiling the second .NET assembly to disk, because we are not referencing it from any other assemblies. Therefore, we can simply use the Add-Type
cmdlet to compile it into a temporary, in-memory assembly. We must make sure that we use the -ReferencedAssemblies
parameter to reference the .NET Assembly that we compiled in Part 1.
# Now that we have a compiled .NET Assembly, we need to write our new code
# that references it (make sure to include all appropriate C# "using" statements)
# Define the second set of C# code
$CsharpCode2 = @"
using Widget;
namespace NASA {
public class Shuttle {
public int Boosters;
public Shuttle() {
this.Boosters = UserCode.GetValue();
}
}
}
"@;
# Try to compile the new code, but wait, we get an error ...
# ... because this code is dependent on the code contained in the
# Widget assembly
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -PassThru;
# We have to reference the .NET Assembly that defines the UserCode type
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -ReferencedAssemblies $OutputAssembly -PassThru;
# Now we can successfully create the NASA.Shuttle object;
$Shuttle = New-Object -TypeName NASA.Shuttle;
# View the number of boosters the Shuttle instance has
Write-Host -Object $Shuttle.Boosters;
This isn't possible with dynamic assemblies (which are generated by default if you don't specify other parameters.)
You can generate dlls with Add-Type and reference them later, so something like:
Add-Type -OutputAssembly foo1.dll ...
Add-Type -ReferencedAssemblies foo1.dll ...
should work.