waf / CSharpRepl

A command line C# REPL with syntax highlighting – explore the language, libraries and nuget packages interactively.
https://fuqua.io/CSharpRepl/
Mozilla Public License 2.0
2.92k stars 113 forks source link

Error CS1109: Extension methods must be defined in a top level static class #283

Closed rahadurr closed 1 year ago

rahadurr commented 1 year ago

Version

0.6.3

What happened?

When I try to implement an extension method it throws the following exception.

│ (3,23): error CS1109: Extension methods must be defined in a top-level static class; StringExtension   │
│ is a nested class

Environment:

.NET SDK: Version: 8.0.100-preview.5.23303.2 Commit: 3fe444af72

Runtime Environment: OS Name: Mac OS X OS Version: 13.4 OS Platform: Darwin RID: osx.13-arm64 Base Path: /usr/local/share/dotnet/sdk/8.0.100-preview.5.23303.2/

.NET workloads installed: There are no installed workloads to display.

Host: Version: 8.0.0-preview.5.23280.8 Architecture: arm64 Commit: bc78804f5d

.NET SDKs installed: 6.0.411 [/usr/local/share/dotnet/sdk] 7.0.305 [/usr/local/share/dotnet/sdk] 8.0.100-preview.5.23303.2 [/usr/local/share/dotnet/sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.19 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0-preview.5.23302.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.19 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0-preview.5.23280.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Screenshot 2023-07-10 at 10 32 15 AM

waf commented 1 year ago

Hi @rahadurr, the reason that doesn't work is ultimately due to a limitation in the underlying C# libraries that CSharpRepl uses.

However, there's an even better way: define the extension method directly in CSharpRepl, without the enclosing class:

image