oleg-shilo / cs-script.net-framework

A mirror of the oleg-shilo/cs-script repository of CS-Script for .NET Framework. A copy of the repo before the product migration on .NET 5migrat
MIT License
30 stars 10 forks source link

The LoadCode function cannot be loaded #7

Closed Davider-code closed 1 year ago

Davider-code commented 1 year ago

hello, I tried to convert the following code in the chm document into powershell code, but I received an error message

ERROR: Method invocation failed because [CSScriptLibrary.RoslynEvaluator] does not contain a method named ''.

Is the document outdated? Is there any other solution? Thank you in advance

system: window7 x64 powershell5.1 .NET4.8

.chm code:

dynamic script = CSScript.RoslynEvaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
int result = script.Sum(1, 2);

Powershell code:

using namespace CSScriptLibrary;
Add-Type -Path "$HOME\Desktop\Binaries_for_DOTNET_4.8\CSScriptLibrary.dll"

$code = @"
using System;

public class Script 
{
    public int Sum(int a, int b) 
    {
        return a + b;
    }
}
"@

$script = [RoslynEvaluator]::LoadCode($code)
$result = $script.Sum(1, 2)

Write-Host "Result: $result"

aa

oleg-shilo commented 1 year ago

This repository contains the latest (even though discontinued) version of CS-Script for .NET Framework.

Problems with the conversion of C# code to PS are beyond this project's scope so I suggest you check that your solution works when implemented in C# and then try to convert it to PS.

Building the solution in C# is easy. Create a console app project and add the last .NET Framework targeting CS-Script package v3.30.5.1. Add your code and that's it.

You can also patch (if you really want) CS-SCript binaries manually by replacing the CS-Script DLLs. I have attached the sample project for you.

ConsoleApp13.zip

Davider-code commented 1 year ago

Thank you for your reply. I don't understand the above C # project file.

In the powershell, I can call the static functions in the DLL file, or create an instance object and then call the methods and properties of the object

I am using many third-party. NET DLL class libraries. They are very successful in loading DLLs, calling methods, properties, etc.

I don't know why there are various problems when calling CSScript DLLs. Maybe I did something wrong?

oleg-shilo commented 1 year ago

It looks like you solved it in #6