【问题标题】:Powershell Add-Type suppress creation of .pdb when compiling .dll (C#)Powershell Add-Type 在编译 .dll (C#) 时禁止创建 .pdb
【发布时间】:2014-01-21 23:56:45
【问题描述】:

我正在创建一个 C# hello world DLL 并使用内置的 powershell Add-Type 命令对其进行编译。这样做时,它会在包含 .dll 的目录中创建一个不需要的 .pdb 调试文件。

如何在使用 Add-Type 命令时禁止创建此 .pdb 文件。我知道在 Visual Studio 中我们可以通过一个选项禁用它,但似乎找不到合适的命令行语法。

这是示例 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()

结果

PS C:\Users\Knuckle-Dragger> [Knuckle.Dragger]::Main()
Knuckle-Dragger was Here!

【问题讨论】:

  • 我认为 cmdlet 中没有内置方法可以跳过 pdb 文件,但说实话.. 添加Remove-Item C:\Knuckle.dll -Force 有那么难吗?

标签: c# powershell dll powershell-2.0 pdb-files


【解决方案1】:

当 C# 编译器在调试模式下编译 .NET 程序集时,会输出 PDB 文件。我不知道为什么Add-Type 默认会使用调试行为进行编译,因为这不是我自己注意到的。但是,如果您想明确禁止这种行为,您可以为 C# 编译器指定编译器选项,特别是 /debug-(注意末尾的减号)。

为了指定编译器选项,必须实例化System.CodeDom.Compiler.CompilerParameters.NET类,在其上指定OutputAssemblyCompilerOptions属性,然后将CompilerParameters对象传入-CompilerParameters的参数Add-Type cmdlet。

这里是 /debug compiler parameter 上的 MSDN 文档,以及 CompilerParameters .NET class 上的文档。

注意:您不能在Add-Type 上同时使用-OutputAssembly 参数和-CompilerParameters 参数。因此,您需要在CompilerParameters 对象上指定OutputAssembly 属性,如前所述。下面的示例代码说明了如何执行此操作。

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;

【讨论】:

    【解决方案2】:

    这可能是由环境变量中设置的编译器选项引起的,因为您使用 SDK CMD shell 打开了提示符。它将标准选项加载到环境变量中。

    如果这是原因,只需清除 PowerShell 中的变量 $env:compiler_options=''

    这不会影响 shell 只是会话。

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2015-07-17
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      相关资源
      最近更新 更多