1

Hello I have been trying for long to run a method from a dll I created using the powershell ise. Now I already run my dll from cmd by using 'dotnet foo.dll' but I would like to use the powershell as well.

Here is my code: Program.cs

using System;

namespace ConsoleApp1
{
    public class Program
    { 
        public static void Main()
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        } 
    }
}

Now I have created a new console app with the .net core framework in VS2017 Enterprise. I only got a dll file after building no executable. I have researched online and found how to load the dll file with [Reflection.Assembly]::LoadFile("pathtofile") btw LoadWithPartialName doesn't work for some reason.

So after loading I try to invoke the Main method with [ConsoleApp1.Program]::Main() and i get this error

Unable to find type [ConsoleApp1.Program].
At line:1 char:1
+ [ConsoleApp1.Program]::Main
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (ConsoleApp1.Program:TypeName)    [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

Please help me I can't find any solution to this for hours.

3 Answers3

0

Not sure if this is allowed, but I found the answer here to work:

https://stackoverflow.com/a/12924252/3485669

Essentially, instead of using Reflection, use Add-Type:

Add-Type -Path "<path to your dll>"

After that, you should be able to use [ConsoleApp1.Program]::Main() as you see fit.

Community
  • 1
  • 1
Forty3
  • 2,199
  • 1
  • 14
  • 19
  • Thank you for your answer this just gives me another error. – Nikolas Pitsillos Mar 25 '17 at 12:15
  • Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. At line:1 char:1 + Add-Type -Path "C:\Users\User\Documents\Visual Studio 2017\Projects\C ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCom mand – Nikolas Pitsillos Mar 25 '17 at 12:17
  • @NikolasPitsillos Are you trying to load the source file directly? (ie. `[Reflection.Assembly]::LoadFile("Path\to\program.cs")`) – Mathias R. Jessen Mar 25 '17 at 12:52
  • @NikolasPitsillos - what version of powershell are you using? – Forty3 Mar 25 '17 at 15:32
  • Version - 5.1.14393.953 – Nikolas Pitsillos Mar 25 '17 at 23:00
  • And you are sure you are pointing to the path to your compiled DLL in your call to Add-Path? And not your .CS file? – Forty3 Mar 25 '17 at 23:14
0

You try to download .Net core assembly to Powershell, but it uses Net Framework.

Use this command:

add-type -Path C:\PathToYourDll\DllName.dll -PassThru

You will see something like this:

"add-type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

After execute this:

$Error[0].Exception.LoaderExceptions

"Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of i
ts dependencies. The system cannot find the file specified." 

First you need to load all dependencies for .Net Core. You can find it in your Visual Studio project for dll, under Solution Explorer->Dependencies->SDK.

But it can by done very easy, if you make you dll not as .Net Core project, just .Net Framework project. In this case you need just download your dll:

And execute function from dll:

techguy1029
  • 743
  • 10
  • 29
Mihail Kuznesov
  • 555
  • 2
  • 13
0

YOUR DLL IS NOT .NET FRAMEWORK DLL, IT IS .NET CORE DLL

You must manually resolve all dependencie

Your dll is .Net core. You must firstly load all .Net core dependencies into your powershell session. You can check missed dependencies by

add-type -Path C:\PathToYourDll\DllName.dll -PassThru
$Error[0].Exception.LoaderExceptions

You must use .net core project? Because for net core dll is something different to load it into powershell session.

Mihail Kuznesov
  • 555
  • 2
  • 13