附加型无法与NLOG类型的引用一起编译C#源
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论