commandlineparser / commandlineparser/commandline

Property with init-only setter results in MissingMethodException

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

描述

**Describe the bug**
In case of read-only properties in the command line options class, where a property is defined as
```
public string Foo
{
get;
init;
}
```
you will get a `MissingMethodException` when parsing the command line.

**To Reproduce**
See this fiddle https://dotnetfiddle.net/J05g0c

**Expected behavior**
The exception is not thrown

**Additional context**
The resulting stack trace is as follows:
```
Unhandled exception. System.MissingMethodException: Cannot dynamically create an instance of type 'Options'. Reason: No parameterless constructor defined.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceOfT()
at System.Activator.CreateInstance[T]()
at CommandLine.Core.InstanceBuilder.<>c__1`1.b__1_0(Func`1 f)
at CSharpx.MaybeExtensions.MapValueOrDefault[T1,T2](Maybe`1 maybe, Func`2 func, T2 noneValue)
at CommandLine.Core.InstanceBuilder.Build[T](Maybe`1 factory, Func`3 tokenizer, IEnumerable`1 arguments, StringComparer nameComparer, Boolean ignoreValueCase, CultureInfo parsingCulture, Boolean autoHelp, Boolean autoVersion, Boolean allowMultiInstance, IEnumerable`1 nonFatalErrors)
at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args)
at Program.Main(String[] args)
```

贡献指南

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

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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