FluentCommandLineParser参数解析时发生了System.InvalidCastException异常。

3
我试图使用 Fclp 解析参数,但遇到以下错误:

System.InvalidCastException:“无法将类型为'System.Reflection.RtFieldInfo'的对象强制转换为类型'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);
    }

1
非常感谢!!!那解决了问题!!如果您想将评论发布为答案,这样我就可以标记它为已解决。@Fildor - Giannis Poriazis
1个回答

3
在您的ApplicationArguments类中,您有公共字段而不是属性。尝试将它们改为自动实现的属性(例如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; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接