Powershell Add-Type在编译.dll(C#)时禁止创建.pdb [英] Powershell Add-Type suppress creation of .pdb when compiling .dll (C#)
问题描述
我正在创建一个C#hello world DLL,并使用内置的powershell Add-Type命令对其进行编译.这样做时,它将在带有.dll的目录中创建一个不需要的.pdb调试文件.
I am creating a C# hello world DLL and compiling it with the built-in powershell Add-Type command. When doing so, it creates an unwanted .pdb debug file in the directory with the .dll.
使用添加类型命令时,如何禁止创建此.pdb文件.我知道在Visual Studio中我们可以通过一个选项禁用它,但是似乎找不到合适的cmd行语法.
How do I suppress creation of this .pdb file when using Add-Type command. I know in Visual studio we can disable that via an option, but cannot seem to find a proper cmd line syntax.
这是示例powershell代码.从控制台运行,它将与.pdb
Here is the example powershell code. Run from console and it will create the DLL on C:\ along with the .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()
结果
PS C:\Users\Knuckle-Dragger> [Knuckle.Dragger]::Main()
Knuckle-Dragger was Here!
推荐答案
C#编译器在调试"模式下编译.NET程序集时,将输出PDB文件.我不知道为什么默认情况下 Add-Type
将使用调试行为进行编译,因为这不是我自己注意到的东西.但是,如果要显式抑制此行为,则可以为C#编译器指定编译器选项,特别是/debug-
(注意末尾的减号).
PDB files are output when the C# compiler is compiling a .NET Assembly in Debug mode. I don't know why Add-Type
would be compiling with debug behavior by default, as this is not something I have noticed myself. However, if you want to explicitly suppress this behavior, you can specify compiler options, specifically the /debug-
(note the minus sign at the end), to the C# compiler.
为了指定编译器选项,必须实例化 System.CodeDom.Compiler.CompilerParameters
.NET类,指定 OutputAssembly
和 CompilerOptions
属性,然后将 CompilerParameters
对象传递到 Add-Type
cmdlet的 -CompilerParameters
参数中.
In order to specify the compiler options, you must instantiate the System.CodeDom.Compiler.CompilerParameters
.NET class, specify the OutputAssembly
and CompilerOptions
properties on it, and then pass the CompilerParameters
object into the -CompilerParameters
parameter of the Add-Type
cmdlet.
这是 /debug
上的MSDN文档.编译器参数,以及
Here is the MSDN documentation on the /debug
compiler parameter, and the documentation for the CompilerParameters
.NET class.
注意:不能在 Add-Type
上将 -OutputAssembly
参数与 -CompilerParameters
参数一起使用.因此,您将需要在 CompilerParameters
对象上指定 OutputAssembly
属性,如前所述.下面的示例代码说明了如何执行此操作.
Note: You cannot use the -OutputAssembly
parameter on Add-Type
alongside the -CompilerParameters
parameter. Therefore, you will need to specify the OutputAssembly
property on the CompilerParameters
object, as previously discussed. The example code below indicates how to do this.
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;
这篇关于Powershell Add-Type在编译.dll(C#)时禁止创建.pdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!