Knu*_*ger 1 c# dll powershell powershell-2.0 pdb-files
我正在创建一个C#hello world DLL并使用内置的PowerShell Add-Type命令对其进行编译.执行此操作时,它会在.dll目录中创建一个不需要的.pdb调试文件.
使用Add-Type命令时,如何禁止创建此.pdb文件.我知道在Visual Studio中我们可以通过一个选项禁用它,但似乎找不到合适的cmd行语法.
这是示例PowerShell代码.从控制台运行,它将在C:\上创建DLL以及.pdb
Clear-Host
Add-Type -OutputAssembly C:\Knuckle.dll @"
using System;
namespace Knuckle
{
public class Dragger
{
public static void Main()
{
Console.WriteLine("Knuckle-Dragger was Here!");
}
}
}
"@
[Void][Reflection.Assembly]::LoadFile("C:\Knuckle.dll")
[Knuckle.Dragger]::Main()
Run Code Online (Sandbox Code Playgroud)
结果
PS C:\Users\Knuckle-Dragger> [Knuckle.Dragger]::Main()
Knuckle-Dragger was Here!
Run Code Online (Sandbox Code Playgroud)
当C#编译器在调试模式下编译.NET程序集时,将输出PDB文件.我不知道为什么Add-Type默认情况下会使用调试行为进行编译,因为这不是我自己注意到的.但是,如果要显式禁止此行为,可以指定编译器选项,特别/debug-是C#编译器(请注意末尾的减号).
要指定编译器选项,必须实例化System.CodeDom.Compiler.CompilerParameters.NET类,在其上指定OutputAssembly和CompilerOptions属性,然后将该CompilerParameters对象传递-CompilerParameters给Add-Typecmdlet 的参数.
以下是有关/debug编译器参数的MSDN文档以及CompilerParameters.NET类的文档.
注意:您不能在-OutputAssembly参数Add-Type旁边使用-CompilerParameters参数.因此,您需要OutputAssembly在CompilerParameters对象上指定属性,如前所述.下面的示例代码说明了如何执行此操作.
mkdir -Path c:\test;
$Code = @"
using System;
namespace test { };
"@
# 1. Create the compiler parameters
$CompilerParameters = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters;
# 2. Set the compiler options
$CompilerParameters.CompilerOptions = '/debug-';
# 3. Set the output assembly path
$CompilerParameters.OutputAssembly = 'c:\test\Knuckle.dll';
# 4. Call Add-Type, and specify the -CompilerParameters parameter
Add-Type -CompilerParameters $CompilerParameters -TypeDefinition $Code;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1202 次 |
| 最近记录: |