oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.56k stars 234 forks source link

Error with extension methods using Roslyn #361

Closed rconde01 closed 5 months ago

rconde01 commented 5 months ago

I apologize I don't have time to create an issue with more detail but I ran into this yesterday. I have cs-script 4.8.7.0 and I tried to run a script, but the dotnet 7 sdk was not installed so it fell back to roslyn. My script structure was:

class Program {
}

static class Extensions {
}

And it failed to run saying that Extensions was a nested class which wasn't allowed for extension methods. Installing dotnet 7 sdk fixed the issue.

oleg-shilo commented 5 months ago

It's not a defect. It's a limitation of Roslyn. It does not support nested classes. Roslyn is one of the supported compiling engines and it unfortunately has this limitation. If you ask me it is rather a design mistake of Roslyn team. But... of course, it's debatable. :)

rconde01 commented 5 months ago

But…it’s not nested? I assumed maybe you’re autogenerating a class around those classes and that was the problem. But this script works with older (probably framework based) versions of cs-script.

oleg-shilo commented 5 months ago

It is nested. Roslyn always creates a class around the code that you ask it to compile. And this is the Roslyn team decision that I mentioned. Roslyn always does it even if it is hosted under .NET Framework.

And yes, you are correct, if you are not using Roslyn as an engine then you can execute your script and normal C# (even with the latest CS-Scrpt). CSScript.CodeDomEvaluator will handle the task just fine. But yes, you will need to have SDK installed for that.

rconde01 commented 5 months ago

Ah I get it…thanks