Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - CS-Script - Packages for Framework 4.8? #402

Open
CDNConsultant opened this issue Jan 2, 2025 · 5 comments
Open

Question - CS-Script - Packages for Framework 4.8? #402

CDNConsultant opened this issue Jan 2, 2025 · 5 comments
Labels

Comments

@CDNConsultant
Copy link

Hello,

I have a .net 4.8 Framework app that I am adding the ability for my end user to do some minor scripting. I have installed the NuGet package CS-Script.Core - and it works for the first few scripts I created (Great tool - thank you for creating it!).

However, I am getting a very strange error when I write certain code.
I have tested the "Script" by including it in my program (called when I click a button) and it works fine! However, when I run the exact same code in CS-Script - it tells me that in the sample code below - that .Distinct() is not found?

error CS1061: 'List' does not contain a definition for 'Distinct' and no accessible extension method 'Distinct' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)
It also gives me an error on the .OrderBy - but I think that is part of the same issue.

Should CS-Script.Core work with a .Net 4.8 Framework project?

Here is a snippet of my script - so that you can see roughly what I was doing - basically just messing with List to write a list of City names out to a file (sorted).

                          List<string> CustNameList = new List<string>();
                          foreach (DataRow InvoiceRow in Invoices.Rows)
                          {
                               CustNameList.Add(InvoiceRow["City"].ToString());
                          }
			 // simply way to remove duplicates from a list
			 List<string> noDupes = CustNameList.Distinct().ToList();
			 noDupes = noDupes.OrderBy(q => q).ToList();

			 // write the list out to a file
			 System.IO.File.WriteAllLines(@"c:\temp\SavedCustomerList.txt", noDupes);

NOTE: If I install CS-Script (4.8.21) - I get a message
Could not load file or assembly 'System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Yet - I have gone and made sure I have all the dependencies at the correct level... I am a little puzzled here - does this package support .Net 4.8 (I noticed that it says .Net 5.0 and higher in the description - but then again CS-SCript.Core also notes .Net 5 ?).

Bradley MacDonald

@oleg-shilo
Copy link
Owner

Hi Bradley,

Let's demystify it.
The error CS1061: 'List' does not contain a definition... is easy to fix. The working sample is attached.
ConsoleApp1.zip

Now, the more important question. .NET Framework vs .NET Core.

All CS-Script library (CS-Script) packages are compiled for netstandard2.0. Meaning that you can host the library in any .NET runtime. Including .NET Framework v4.8. However. The CS-Script depends/uses C# compiler service Roslyn, which, sins not so long ago, stopped supporting .NET Framework. That's why you have "System.Runtime.Loade" error. Meaning that the latest CS-Script can only work on .NET Core family runtimes (.NET 5 and later).

Thus if you use the older version of CS-Script nuget package you will not have any problems hosting it but not the latest one. I do not remember exactly which version though.

You are using the CS-Script.Core package, which is old enough and works well in .NET Framework apps.

Historically, CS-Script.Core was the very first milestone release of the current CS-Script architecture. Later, when MS renamed '.NET Core' into '.NET', and then the CS-Script.Core package was renamed into CS-Script.

Where does it leave you?
You can either stay with the package you have. Or find some letter version that works for .NET Framework.

And of course you may consider migration your app on the .NET Core

@oleg-shilo
Copy link
Owner

oleg-shilo commented Jan 2, 2025

I quickly checked and the package CS-Script v4.8.1 is the last one that supports .NET Framework.

@CDNConsultant
Copy link
Author

Oleg, Thank you for the above explanations!! That really does demystify things :)

I will try out the Zip file later today (have to do my real job first :) )

I really appreciate your time on this. I do plan on migrating to the latest Dot Net - but I needed to get it out the door in the next month - and while it shouldn't be that difficult - it would have delayed my hopeful Feb 1st release date. I mainly use DevExpress, Active Query Builder - and the DevArt components, which all support the latest Dot Net versions - so I am hopeful...

I truly do appreciate your support here (as a shareware author I can appreciate the time it takes!). I would like to offer you a free license to my two programs - a disk space manager - and a new one that allows you to produce power grids, pivot grids, graphs, dashboards and reports from your data (database, csv, Excel, etc.). With this one - you can see how I am using CS-Script to allow my users to customize their data - if you are curious about it. You can reach me at brad AT cdnconsultant DOT com. I would happily send you free licenses!! (Oh btw - the website is down right now while it is being retooled. Hope to have it up middle of January)
Have a great day!
Bradley

@CDNConsultant
Copy link
Author

Oleg,
Ok - that sample worked great. I was using CSSCript.Evaluator.LoadCode - then running the script. However, that was not working properly :)

I changed it from my code - to yours - and it worked really well.

Bad -->                     dynamic script = CSScript.Evaluator.LoadCode(CurrCode);
                            dynamic script = CSScript.RoslynEvaluator.LoadMethod(CurrCode);
                            ReturnValue = script.AfterTableProcess(CurrDataSet, CurrTableName, MainForm);

What is the difference between using the Evaluator.LoadCode and the Roslyn.LoadMethod ? I am guessing that one is if you are loading one method (which I am guessing can take sub-methods in the code) or are loading an entire class structure??
Although I am puzzled as to why the LoadCode did not like the code - although I am guessing that it was not using Roslyn code?

I think I like the LoadMethod better at this point - keeps my scripts looking much leaner.
Thank you again!

Bradley

@oleg-shilo
Copy link
Owner

It is the same operation. LoadMethod simply wraps the method into a class and calls LoadCode.
This code below will work with LoadCode too:

dynamic script = CSScript.RoslynEvaluator
                         .LoadCode(@"
                            using System.Collections.Generic;
                            using System.Linq;
                            public class Script
                            {
                                public string func()
                                {
                                    List<string> CustNameList = new List<string>();
                                    CustNameList.Add(""test1"");
                                    CustNameList.Add(""test1"");
                                    CustNameList.Add(""test2"");

                                    // simply way to remove duplicates from a list
                                    List<string> noDupes = CustNameList.Distinct().ToList();
                                    noDupes = noDupes.OrderBy(q => q).ToList();

                                    // write the list out to a file
                                    // System.IO.File.WriteAllLines(@""c:\temp\SavedCustomerList.txt"", noDupes);

                                    return string.Join("","", noDupes.ToArray());
                                }
                            }");

var result = script.func();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants