-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion "Support extension methods everywhere" #301
Comments
See also dotnet/roslyn#16271 |
If this is an issue it can be made to accept only private methods for non-static classes. |
Not being able to create extension methods in non-static classes prevents me from using dependency-injected logging inside the extension methods. This makes unit testing difficult and forces me to break things out into services, which adds more complexity than is called for. Being able to do this would make things much simpler: public class ModelConversionExtensions
{
private readonly ILogger<ModelConversionExtensions> _logger;
public ModelConversionExtensions(ILogger<ModelConversionExtensions> logger) => _logger = logger;
public static DataModel? ConvertToDataModel(this ViewModel vm)
{
try
{
return new DataModel() { foo_bar = vm.FooBar };
}
catch
{
_logger.LogError($"Could not convert {nameof(ViewModel)} with Id of {vm.Id} to {nameof(DataModel)}");
return null;
}
}
} |
A static member cannot access instance members without an instance. You cannot accomplish what you're describing without some kind of global state, which is pretty much faux pas in a dependency/unit paradigm. However, you could easily accomplish it via: public static class ModelConversionExtensions
{
private static ILogger<ModelConversionExtensions> _logger = SomeDefaultLogger;
public static void RegisterLogger(ILogger<ModelConversionExtensions> logger) => _logger = logger;
public static DataModel? ConvertToDataModel(this ViewModel vm)
{
try
{
return new DataModel() { foo_bar = vm.FooBar };
}
catch
{
_logger.LogError($"Could not convert {nameof(ViewModel)} with Id of {vm.Id} to {nameof(DataModel)}");
return null;
}
}
} |
@HaloFour You're totally right, that's my bad. Thanks for the suggestion. In your example, what does |
See also dotnet/roslyn#4565
I've labeled this "Proposal Champion" because it has been triaged by the LDM, but we don't actually have a champion at this point.
The text was updated successfully, but these errors were encountered: