System.InvalidCastException 正在解析 FluentCommandLineParser 参数

System.InvalidCastException parsing FluentCommandLineParser arguments

我尝试使用 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 class 中,您有 public 个字段,而不是属性。尝试使它们成为 auto-implemented 属性(例如 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; }
}