1

I'm using powershell core 7.4.2 to run an own .Net module

[Reflection.Assembly]::LoadFile("C:\test\test.dll")
[Some.NameSpace.ClassName]::DoSth('...')

If test.dll is compiled for .Net Framework 4.8 this works as expected.

If test.dll is compiled for .Net 8.0 this fails. Fiddling around with Add-Type reveals a ReflectionTypeLoadException. Some parts of the assembly are missing

try
{
    Add-Type -Path "C:\test\test.dll"
}
catch [System.Reflection.ReflectionTypeLoadException]
{
    $e = $PSItem.Exception
    $e.LoaderExceptions
}

None of the listed assemblies in LoaderExceptions are used in my code (e.g. System.Text.RegularExpressions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) but more of interest is "System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which indicates to me that basically .Net 8 is not loaded at all.

I did some more tests by adding the assemblies in question System.Runtime.dll, System.Private.CoreLib.dll, System.Text.RegularExpressions.dll but gave up after the error that "System.Object" of the assembly "System.Private.CoreLib" can not be found.

test.dll does not include fancy usings:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Win32;

namespace Some.NameSpace
{
    public static class ClassName
    {
        public static void DoSth(string fileName)
        {
        }
    }
}

What am I doing wrong?

3
  • What version do you see if you do [System.IntPtr].Assembly.Location |Get-ItemPropertyValue -Name VersionInfo? Commented May 3, 2024 at 10:14
  • @MathiasR.Jessen Good tip. It seems that inoking powershell from cmd uses the .Net 4 one: 4.8.9232.0 4.8.9232.0 bu... C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll. Then it is clear why the assembly can't be loaded. Need to investigate a bit more... Commented May 3, 2024 at 11:25
  • 1
    Sounds like maybe you're launching powershell.exe (Windows PowerShell 5.1) instead of pwsh.exe (PowerShell 7)? Commented May 3, 2024 at 11:26

1 Answer 1

2

I was running powershell.exe rather than pwsh.exe.

Powershell.exe uses .Net Framework 4.8
Pwsh.exe 7.2.x uses .NET 6, the 7.3.x uses .NET 7 and the 7.4.x uses .NET 8

Sometimes small things make a big difference and cost hours of finding an obvious issue...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.