commandlineparser / commandlineparser/commandline

C#8 Support for Default Interface in Options

未关闭
#696 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C#
星标
4.8k
派生
478
PR 合并指标
30 天内没有已合并 PR

描述

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...?

贡献指南

这个仓库没有索引到贡献指南

调研方向

复现 Issue 中的 C# 8 default-interface 示例,并确认解析该选项时的 null 结果。由于没有指定源文件或测试,首先定位选项发现和解析的入口点;当所请求的组合方式有 regression test 支持,或已明确记录为不支持时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp
领域
cli
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。