C# 在powershell类中使用dotnet接口

C# 在powershell类中使用dotnet接口,c#,powershell,class,C#,Powershell,Class,我想在powershell中使用自定义.net库中的接口,但总是出现以下错误: class Logger : Systems.SysBiz.BaseTransversal.ILoggerBl + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Unable to find type [Systems.SysBiz.BaseTransversal.ILoggerBl]. 例如,如果我使用系统名称空间中的IComparable接口,它

我想在powershell中使用自定义.net库中的接口,但总是出现以下错误:

class Logger : Systems.SysBiz.BaseTransversal.ILoggerBl
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Systems.SysBiz.BaseTransversal.ILoggerBl].
例如,如果我使用系统名称空间中的IComparable接口,它就可以正常工作

[System.Reflection.Assembly]::LoadFile('MyPath\BaseTransversal.dll')

class Logger : Systems.SysBiz.BaseTransversal.ILoggerBl
{

    [bool] $SendMailException = $false;

    Logger()
    {

    }

    [void] LogFatal([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [void] LogException([System.Exception] $ex, [string] $message, [object[]] $args)
    {
        Write-Host $this.FormatException($ex);            
    }

    [void] LogError([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [void] LogWarning([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [void] LogInfo([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [void] LogTrace([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [void] LogDebug([string] $message, [object[]] $args)
    {
        Write-Host $message;
    }

    [string] FormatException([System.Exception] $ex)
    {
        if (!$ex)
        {
            return "";
        }

        return "<br/><b>" + $ex.Message + "</b><p>" + $ex.StackTrace + "</p><br/>" + $this.FormatException($ex.InnerException);
    }
}
更新:添加了完整类powershell代码 我也尝试过在我的.net代码中使用空接口,但看起来我无法看到/使用我的接口,即使它们是公共的。我错过什么了吗


更新2:添加了精确错误

我也遇到了同样的问题。PowerShell类在脚本评估开始之前进行处理。因此,在类尝试从中继承之前,第一行不会运行,因此没有定义类型。解决方案是将类和加载移动到两个单独的脚本中。然后,在形成入口点的第三个脚本中点源代码。加载定义外部类型的脚本。然后加载定义类的脚本,这样在第二个脚本开始计算之前导入类型

# in file typeLoad.ps1
Add-Type -TypeDefinition "
public interface IHaveName {
    string Name { get; set; }
}"

# in file classes.ps1
class B : IHaveName {

}

# in file main.ps1
. $PSScriptRoot\typeLoad.ps1
. $PSScriptRoot\classes.ps1

你装上你的组件了吗?您的问题似乎是一个调试问题,似乎缺少一些帮助我们解决问题的关键信息。是的,我使用[System.Reflection.Assembly]加载dll::LoadFile('C:\Projects\SysBiz New\SysBizClientTester\src\powershell\libs\BaseTransal.dll')。“但我始终得到找不到的类型”-请完整地发布错误消息:):-)很抱歉这个错误。找不到类型[Systems.SysBiz.baseTraversal.ILoggerBl]。哦,是的,它可以工作。但是现在如果我使用this对象,它似乎没有实现接口,因为在这个调用上[Systems.SysBiz.baseTransactional.ILoggerBl]$logger=[logger]::new()$authentication=new Object Systems.SysBiz.authenticationTransactical.Internal.AuthenticationBl“MYHOST”$logger我遇到此错误:找不到接受参数“logger”的位置参数。哦,找到了。在论点列表中有一个不见了
# in file typeLoad.ps1
Add-Type -TypeDefinition "
public interface IHaveName {
    string Name { get; set; }
}"

# in file classes.ps1
class B : IHaveName {

}

# in file main.ps1
. $PSScriptRoot\typeLoad.ps1
. $PSScriptRoot\classes.ps1