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

Allow for Static Extensions #2334

Closed
mills-andrew opened this issue Mar 12, 2019 · 11 comments
Closed

Allow for Static Extensions #2334

mills-andrew opened this issue Mar 12, 2019 · 11 comments

Comments

@mills-andrew
Copy link

mills-andrew commented Mar 12, 2019

I am hoping to get rid of wrapper classes. I love the idea of extension classes but I cannot add an extension method to a static class. For example: I want extension methods for ConfigurationManager. So when i try to add an extension method to a static class I get the error "'ConfigurationManager': static types cannot be used as parameters" for the following code.

public static class ConfigurationManagerExtensions
{
    public static void RunSettings(this ConfigurationManager cManager) { }
}

Doing this forces me to change my class to a wrapper class.

public static class ConfigurationManagerWrapper
{
    public static void RunSettings() { }
}

so now instead of going using the following code:

ConfigurationManager.RunSettings()

Where ConfigurationManger is well known class used in the System.Configuration namespace. I now have to tell my developers to use the Wrapper class

ConfigurationManagerWrapper.RunSettings()

What is the purpose of not allowing this ability to make static extensions? Id love to see them in a future update. I would find this extremely useful

@YairHalberstadt
Copy link
Contributor

Would #1988 solve your issues?

@mills-andrew
Copy link
Author

Would #1988 solve your issues?

Its a similar issue, but there is no solution to that issue.

@YairHalberstadt
Copy link
Contributor

Yes there is. It suggests that you should be allowed to just make your own class called ConfigurationManager and reference both directly.

@mills-andrew
Copy link
Author

mills-andrew commented Mar 12, 2019

Thats a suggestion for a feature that does not exist. Not a solution to the existing features.

@HaloFour
Copy link
Contributor

Not a solution to the existing features.

There is no existing solution for the feature you're looking for. That issue is a proposal to look into adding such a feature. If you have a different idea as to how to implement such a proposal feel free to write one up.

What is the purpose of not allowing this ability to make static extensions? Id love to see them in a future update.

The reason the syntax you attempted failed is because you can't declare a parameter or a variable of a static class. It doesn't make any sense to. Something like "static extension methods" would have to be a new feature that has to go through a separate design process.

@mills-andrew
Copy link
Author

mills-andrew commented Mar 12, 2019 via email

@YairHalberstadt
Copy link
Contributor

YairHalberstadt commented Mar 12, 2019

But the feature suggestion as is makes no sense syntactically. Adding a parameter of a static type is meaningless.

It will have to be reworked syntactically, at which point I imagine it will look something like #1988.

@mills-andrew
Copy link
Author

Yes, and im sure your suggestion works theoretically, but its not a solution. Which is why i said your proposed solution is just that, a proposal, not a solution.

@HaloFour
Copy link
Contributor

This will likely fall out naturally from the discussion/proposal around "extension everything" which aims to expand on extension methods to support other members, both static and instance, and possibly even interface implementations:

#192
dotnet/roslyn#11159
#1711

@juliusfriedman
Copy link

juliusfriedman commented Mar 31, 2019

But can't you write a more conventional static method and just drop the this?

Yeah you won't get intellisense on that type (Configuration manager) but you will get it from the namespace where the static function is imported... E g. ConfigurationManagerHelper...

Optionally use composition to make a class which gets a ConfigurationManager and add you methods to that class.

I wouldn't say this feature makes no sense it's just tricky especially when using constructs are in play and requires us to jump through hoops when we encounter this paradigm.. e.g. ServicePointManager et al

@YairHalberstadt
Copy link
Contributor

Unfortunately I can't move this issue to a discussion due to a Github bug.

I'll close it instead as I think this area is suitably covered by #192 and #1711

If you wnat to discuss this more, feel free to open a new discussion and link this issue.

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

No branches or pull requests

4 participants