附加型无法与NLOG类型的引用一起编译C#源

发布于 2025-01-30 18:11:49 字数 2319 浏览 1 评论 0原文

我正在尝试使用C#源添加PowerShell V5中的类型,该源引用NLOG v5.0.0记录库。

当我执行以下MRE时:

$source = @'
namespace Test
{
    using System;

    public class MyClass
    {
        private NLog.ILogger logger;

        public static void Run()
        {
            Console.WriteLine("Hello world!");
        }
    }
}
'@

$path = 'C:\nlog\net46\NLog.dll'
$assemblies = @($path)

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharpVersion3 -ErrorAction Stop

[Test.MyClass]::Run()

我会收到以下错误:

Add-Type : (0) : Warning as Error: Reference to type 'System.Action' claims it is defined in 'c:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorlib.dll', but it could not be found
(1) : namespace Test
At C:\test.ps1:21 char:1
+ Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -L ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

如果我在类中注释了引用NLOG类型的字段(private nlog.ilogger logger;),则MRE将成功执行。

如果add-type执行的原始代码,则在编译中剩下的字段并执行执行:

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -IgnoreWarnings -ErrorAction Stop

但是,实际上使用库是不足的。以下MRE的失败不同:

$source = @'
namespace Test
{
    using System;

    public class MyClass
    {
        public static void Run()
        {
            Console.WriteLine(typeof(NLog.ILogger));
        }
    }
}
'@

$path = 'C:\nlog\net46\NLog.dll'
$assemblies = @($path)

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -ErrorAction Stop

[Test.MyClass]::Run()

我现在会收到以下错误:

Exception calling "Run" with "0" argument(s): "Could not load file or assembly 'NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The 
system cannot find the file specified."
At C:\test.ps1.ps1:21 char:1
+ [Test.MyClass]::Run()
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

I am trying to add a type in PowerShell v5 using C# source that references the NLog v5.0.0 logging library.

When I execute the following MRE:

$source = @'
namespace Test
{
    using System;

    public class MyClass
    {
        private NLog.ILogger logger;

        public static void Run()
        {
            Console.WriteLine("Hello world!");
        }
    }
}
'@

$path = 'C:\nlog\net46\NLog.dll'
$assemblies = @($path)

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharpVersion3 -ErrorAction Stop

[Test.MyClass]::Run()

I get the following error:

Add-Type : (0) : Warning as Error: Reference to type 'System.Action' claims it is defined in 'c:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorlib.dll', but it could not be found
(1) : namespace Test
At C:\test.ps1:21 char:1
+ Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -L ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

If I comment out the field in the class that references the nlog type (private NLog.ILogger logger;), the MRE executes successfully.

The original code with the field left in compiles and executes if Add-Type is executed as follows:

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -IgnoreWarnings -ErrorAction Stop

However, that is not sufficient to actually use the library. The following MRE fails differently:

$source = @'
namespace Test
{
    using System;

    public class MyClass
    {
        public static void Run()
        {
            Console.WriteLine(typeof(NLog.ILogger));
        }
    }
}
'@

$path = 'C:\nlog\net46\NLog.dll'
$assemblies = @($path)

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -ErrorAction Stop

[Test.MyClass]::Run()

I now get the following error:

Exception calling "Run" with "0" argument(s): "Could not load file or assembly 'NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The 
system cannot find the file specified."
At C:\test.ps1.ps1:21 char:1
+ [Test.MyClass]::Run()
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文