question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Extension method support in scripting

See original GitHub issue

Problem

Developers using C# scripting are unable to define extension methods in script. I believe this is due to the fact that the script code is wrapped in the submission, and you cannot have extensions on nested static classes. Also Roslyn does not allow using “this”

For example, a developer cannot do this within a script.

public static class StringExtensions() {
  public static string DoSomething(this string text ) {
    ...
  }   
}

Now extension methods can be defined on dlls and loaded into the scripting context, however this requires a developer to create a full blown C# project / dll. It would offer a smoother scripting experience if these extension methods could be defined within a top-level script, or a child script that is loaded with #load.

Proposed solutions

Both solutions would require allowing usage of the this pointer in script, OR some equivalent. If this is enabled, it could be scoped to only work for extension methods if there is concern about its usage elsewhere.

A. Support extension methods on nested classes.

If extension methods are enabled on nested static classes, then one could define a static class in script with extensions and they would be picked up. This would make the above example work, though I imagine it will be more work to allow this at the compiler level.

B. Lift the class at compile time

Provide a new #extensions directive which can be put on a class to indicate that this class has extension methods. At compile time the class is lifted out of the submission so that the extension methods can be resolved.

Here is an example of how this would look from an authoring perspective

#extensions
public static class StringExtensions() {
  public static string DoSomething(this string text ) {
    ...
  }   
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tmatcommented, Jan 29, 2015

My opinion is that we should allow to define extension methods in any class in C# proper.

0reactions
glennblockcommented, Jan 7, 2016

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where is the proposal for standardization of "extension ...
There is a proposal for Unified (var Uniform) Call Syntax for C++. From 10,000 feet PoV, it will allow extensions methods a la...
Read more >
Using Extension Methods in C# to Build Fluent Code
In this article I'll attempt to demystify extension methods and illustrate how they can be used to build elegant and fluent code.
Read more >
Why I Don't Like C# Extension Methods
The advantages are immediately obvious. You can abstract verbose and redundant common idioms into these methods, favoring instead descriptive ...
Read more >
Extension Methods
I made a couple of extension methods to help with the playing of animations as I felt there were missing. Such as playing...
Read more >
Using extension points to extend application functionality
Use extension points to extend the functionality of an application without altering the original application code. You can use pre-existing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found