zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Console error #142

Open kller1993 opened 1 year ago

kller1993 commented 1 year ago

Unhandled exception. Z.Expressions.Compiler.Shared.EvalException: Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 69 near ".ReadLine(); Console.W".

Apparently the ReadLine from Console.ReadLine isnt a recognized member, unlike the WriteLine.

Edit: Error appeared after creating a loop to register the used members, etc. In this case:

using System; using System.Data; using System.Data.Odbc; using System.Data.SqlTypes; using System.Xml; using MySqlConnector;

JonathanMagnan commented 1 year ago

Hello @name,

Can you provide a runnable project/solution or a Fiddle with the issue you describe? You can send private files here: info@zzzprojects.com

We were not able to reproduce this, and we are not sure how registering those members will affect the Console.ReadLine. Perharp we are doing something different.

Best Regards,

Jonathan

kller1993 commented 1 year ago

using Z.Expressions; { int linec = 1; int linec2 = 1; string code = ""; string[] cmd = File.ReadAllLines(//Program//); foreach (string line in cmd) { if (!line.Contains("//")) { if (line.StartsWith("using")) { string dll1 = line.Remove(0, 6); Type dll = (dll1.Substring(0, dll1.Length - 1)).GetType(); EvalManager.DefaultContext.RegisterType(dll); EvalManager.DefaultContext.RegisterAssembly(dll.Assembly); EvalManager.DefaultContext.RegisterExtensionMethod(dll); EvalManager.DefaultContext.RegisterMember(dll); } else { Console.WriteLine(Convert.ToString(linec2).PadLeft(10, ' ').PadRight(15, ' ') + Convert.ToString(linec).PadLeft(10, ' ').PadRight(15, ' ') + line); code = code + line; linec2++; } } linec++; } var t = Eval.Execute(code); }

Is a testing setup. Am trying to get it running with every possibility added one after another. In cmd[] currently is written the file of the code to import, later it will be a Console.ReadLine(); And the Console.WriteLine is for controlling the code running.

JonathanMagnan commented 1 year ago

Hello @kller1993 ,

My developer tried to reproduce the issue with the same code as you but still is not able to reproduce it.

So I believe a runnable project will be required (you can send private files here: info@zzzprojects.com) as we currently miss something to have the same behavior as you.

Best Regards,

Jon

kller1993 commented 1 year ago

Hello @JonathanMagnan ,

I just want to confirm, if you received the Mail with the .rar of the Project. I included everything, even the lines I use to de/activate parts of code for testing. I just want to confirm, as the first Mail got blocked, while for the second I still havent got any response.

Best Regards,

kller1993

JonathanMagnan commented 1 year ago

Hello @kller1993 ,

We indeed got your project. I will answer directly to your email.

For people looking for the issue cause:

In the code, the RegisterAssembly was used for the type string assembly. However, doing it was also registering the assembly System.Private.CoreLib, which contains an Internal.Console. The Console from the Internal.Console was used, which doesn't contains the ReadLine method.

One solution instead to try to register all types is using the AutoAddMissingTypes options: https://eval-expression.net/options#autoaddmissingtypes

kller1993 commented 1 year ago

Hello @JonathanMagnan ,

Am wondering, where my mistake is, as Auto doesnt seem to work.

public class TestingArea
{
    public static void Main()
    {
        var context = new EvalContext();
        context.RegisterAutoAddMissingTypeAssembly(typeof(TestingArea).Assembly);
        context.AutoAddMissingTypes = true;
        {
            var load = context.Execute<string>("TestingArea.Loader()");
            var t = context.Execute(load);
            Console.WriteLine("EndTest");
        }
    }
    public static string Loader()
    {
        int linec = 1;
        int linec2 = 1;
        string code = "";
        Console.WriteLine("Path:");
        string path = Console.ReadLine();
        string[] cmd = File.ReadAllLines(path);
        foreach (string line in cmd)
        {
            if (!line.Contains("//"))
            {
                if (!line.StartsWith("using"))
                {
                    //Console.WriteLine(Convert.ToString(linec2).PadLeft(10, ' ').PadRight(15, ' ') + Convert.ToString(linec).PadLeft(10, ' ').PadRight(15, ' ') + line);
                    code = code + line;
                    linec2++;
                }
            }
            linec++;
        }
        return code;
    }
}

It doesnt register at least types.

Edit: Tried if adding

var context = new EvalContext();
        context.RegisterAutoAddMissingTypeAssembly(typeof(TestingArea).Assembly);
        context.AutoAddMissingTypes = true;

before the imported Code helps, but it only changed to missing the EvalContext type.

JonathanMagnan commented 1 year ago

Hello @kller1993 ,

The first part to use TestingArea.Loader() work perfectly. Here is an online example very similar to your: https://dotnetfiddle.net/kOqaUM (I changed TestingArea to Program to make it compatible with Fiddle

I suspect what is not working is what is the code that will be returned by the Loader method.

A cause could also be that whenever you specify an assembly in the RegisterAutoAddMissingTypeAssembly, that means that missing types can only be retrieved from this assembly instead of all loaders assembly. That could maybe explain the issue.

If you could provide a full runnable example, that will surely be easier on our side to tell you exactly why this is not working.

Best Regards,

Jon

JonathanMagnan commented 1 year ago

Hello @kller1993 ,

Since our last conversation, we haven't heard from you.

How is everything going?

Let me know if you need further assistance.

Best regards,

Jon

JonathanMagnan commented 1 year ago

Hello again,

A simple reminder that we are here to assist you.

Don't hesitate to contact us if you need anything.

Best regards,

Jon