我try 使用Fclp分析参数,但出现以下错误:

System.InvalidCastException: 'Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'.'

你知道是什么引起的吗?我传递给控制台的参数是-D 5

class Program
{
    public class ApplicationArguments
    {
        public int TenantId;
        public int Days;
    }

    static void Main(string[] args)
    {
        var p = new FluentCommandLineParser<ApplicationArguments>();

        p.Setup(arg => arg.TenantId)
            .As('T', "tenantid");

        p.Setup(arg => arg.Days)
            .As('D', "days")
            .Required();

        var result = p.Parse(args);
    }

推荐答案

ApplicationArguments类中,您有公共字段,而不是属性.try 使其成为自动实现的属性(例如public int TenantId { get; set; }).阅读错误消息,这可能会奏效.

此外,这也是FluentCommandLineParser项目自身示例中的内容:https://github.com/fclp/fluent-command-line-parser#usage

引用:

public class ApplicationArguments
{
   public int RecordId { get; set; }
   public bool Silent { get; set; }
   public string NewValue { get; set; }
}

Csharp相关问答推荐

.NET通过版本自动增量设置包版本

使用其可能实现的基类和接口的属性的方法

编写DataAnnotations自定义验证器的多种方法

实现List T,为什么LINQ之后它不会返回MyList?<>(无法强制转换WhereListIterator `1类型的对象)'

如何在Windows 11任务调度程序中每1分钟重复一次任务?

如何使用C#中的主构造函数功能使用多个构造函数?

System.Text.Json .NET 8多形态语法化

如何在NET 8最小API中自动记录TypedResults.Stream响应

如何在CSharp中将json字符串转换为DataTable?

将操作从编辑页重定向到带参数的索引页

具有类型识别的泛型方法

如何在特定环境中运行dotnet测试?

RCL在毛伊岛应用程序和Blazor服务器应用程序.Net 8.0中使用页面

C#如何获取字符串中引号之间的文本?

我是否应该注销全局异常处理程序

Autofac -动态实例化:手动传递构造函数

C#;AvaloniaUI;MVVM;当另一个窗口上的按钮被单击时,如何更新视图图像源?

SignalR跨域

在C#中删除多个不同名称的会话

缩写的MonthNames有问题