commandlineparser / commandlineparser/commandline
C#8 Support for Default Interface in Options
- Lingua principale
- C#
- Stelle
- 4.8k
- Fork
- 478
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
I have a lot of projects reusing the same kind of option. So far I've been using inheritance to achieve some kind of code reuse but it's not perfect and quickly becomes unwieldy.
Since C# 8 supports default interfaces I tried the following (I know a lot of people are horrified by default interfaces but since we can't inherit from more than one abstract class I don't see any other way to implement what I'm looking for (which would be kind of like traits in other languages). If there's another way to do this please let me know.
Let's say we have this StringOptionOne (with a lot of other StringOptions like StringOptionTwo, StringOptionThree, StringOptionFour and we want to be able to use them to compose the Options class.
```
public class Options
{
[Option('s', "stringoptionone", Required = true, HelpText = "The string option one value.")]
public string StringOptionOne { get; set; }
}
```
Defining a regular interface I still have to implement the property and the attributes in each Options and I'm trying to avoid this.
Using inheritance it's impossible to have all the possible variants since we can't inherit from more than one class.
But using Default interface we could have something like this:
```
public interface IDefaultStringOptionOne
{
private static string _stringOptionOne = "";
[Option('s', "stringoptionone", Required = true, HelpText = "The string option one value.")]
public static string StringOption
{
get => _stringOptionOne;
set => _stringOptionOne = value;
}
}
```
And use it like this:
```
public class Options: IDefaultStringOptionOne, IDefaultStringOptionTwo
{
}
```
With C#8 this compiles but I'm getting a null when parsing the option.
Is there a way to support this...? Or another way to compose Options at the property level...?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.