【问题标题】:Custom Assembly is added to the PowerShell Assemblies but still won't compile自定义程序集已添加到 PowerShell 程序集,但仍无法编译
【发布时间】:2015-08-02 00:45:31
【问题描述】:

我正在尝试在 PowerShell 中使用自定义 .NET 程序集运行 C# 代码,但无法使其正常工作。

这是我的代码:

# Adding custom assemblies to the PowerShell assemblies
Add-Type -Verbose -Path "C:\MyCustomAssembly.dll"

# Adding reference of standard .NET assemblies 
$Refs = @("C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll",
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.Entity.dll",
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Runtime.Serialization.dll")

$Source = @"
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using MyCustomAssembly;

namespace CSharpCode
{
    public class WebClient
    {
        public void ConsumePostMethod()
        { ... }
    }
}
"@

Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -PassThru

[CSharpCode.WebClient]::ConsumePostMethod()

这是我得到的错误:

Add-Type : c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(5) : The type or namespace name 'MyCustomAssembly' could not be found (are you missing a using directive 
or an assembly reference?)
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(4) : using System.Runtime.Serialization;
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(5) : >>> using MyCustomAssembly;
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(6) : 
At line:109 char:1
+ Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (c:\Users\MyUser...bly reference?):CompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. There were compilation errors.
At line:109 char:1
+ Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

Unable to find type [CSharpCode.WebClient]: make sure that the assembly containing this type is loaded.
At line:111 char:1
+ [CSharpCode.WebClient]::ConsumePostMethod()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (CSharpCode.WebClient:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

奇怪的是,如果我在下面运行这个命令,它显示 MyCustomAssembly.dll 已在会话中加载。有人可以帮忙吗?!

[System.AppDomain]::CurrentDomain.GetAssemblies() | Where {$_.Location} | ForEach {Split-Path -Leaf $_.Location} | Sort

Accessibility.dll
MyCustomAssembly.dll
Microsoft.Build.Framework.dll
Microsoft.CSharp.dll
...

【问题讨论】:

  • 您应该将MyCustomAssembly 的引用添加到$Refs 变量中。
  • 谢谢,这行得通!!! @PetSerAl 您能否将您的评论重新发布为答案,以便我将其标记为答案?

标签: c# .net powershell using-statement add-type


【解决方案1】:

Add-Type 不使用任何当前加载的程序集作为隐式引用。因此,如果您想在Add-Type 源代码中使用自定义程序集的任何类型,那么您应该将此程序集显式添加到-ReferencedAssemblies 参数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多