To reference the first .NET assembly from the second .NET assembly, you need to transfer the first .NET assembly to disk. Once you do this, you can reference the first .NET assembly from the second .NET assembly using the -ReferencedAssembliescmdlet parameter Add-Type.
. #, , using .
, , :
1 - # 1
.NET , . -OutputAssembly -OutputType Add-Type.
$Csharp = @"
using System;
using System.Reflection;
namespace Widget {
public class UserCode {
public static int GetValue() {
return 2;
}
}
}
"@
$OutputAssembly = '{0}\Widget.dll' -f $env:USERPROFILE;
Add-Type -TypeDefinition $Csharp -OutputAssembly $OutputAssembly -OutputType Library;
[System.Reflection.Assembly]::LoadFile($OutputAssembly);
2 - № 2
.NET , - . Add-Type, . , -ReferencedAssemblies .NET, 1.
$CsharpCode2 = @"
using Widget;
namespace NASA {
public class Shuttle {
public int Boosters;
public Shuttle() {
this.Boosters = UserCode.GetValue();
}
}
}
"@;
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -PassThru;
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -ReferencedAssemblies $OutputAssembly -PassThru;
$Shuttle = New-Object -TypeName NASA.Shuttle;
Write-Host -Object $Shuttle.Boosters;