Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using dotnet interface in powershell class

I want to use in my powershell a interface from my custom .net library, but i got always this error:

class Logger : Systems.SysBiz.BaseTransversal.ILoggerBl
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Systems.SysBiz.BaseTransversal.ILoggerBl].

If I use for example the IComparable interface from the system namespace it works fine.

[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);
    }
}

Here my .net interface

namespace Systems.SysBiz.BaseTransversal
{
    public interface ILoggerBl
    {
        bool SendMailException { get; }
        void LogFatal(string message, params object[] args);
        void LogException(Exception ex, string message, params object[] args);

        void LogError(string message, params object[] args);

        void LogWarning(string message, params object[] args);

        void LogInfo(string message, params object[] args);

        void LogTrace(string message, params object[] args);

        void LogDebug(string message, params object[] args);

        string FormatException(Exception ex);
    }
}

UPDATE: Added full class powershell code I have also tried with a empty interface in my .net code but looks like the i can't see/use my interfaces even they are public. Did I miss something

UPDATE 2: Added exact error

like image 552
cpiock Avatar asked Sep 20 '25 01:09

cpiock


1 Answers

I've had the same problem. PowerShell classes are processed before the evaluation of the script starts. So your first line does NOT run before the class tries to inherit from it, and so the type is not defined. A solution to that is moving the classes and the loading into two separate scripts. Then dot-source them in a third script that forms your entry point. Load the script that defines external types firs. Then load the script that defines your classes, that way the types are imported before the second script starts evaluating.

# 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
like image 63
nohwnd Avatar answered Sep 21 '25 17:09

nohwnd