Home > Net >  System.InvalidCastException parsing FluentCommandLineParser arguments
System.InvalidCastException parsing FluentCommandLineParser arguments

Time:06-01

I try to parse arguments with Fclp and I get the following error:

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

Any ideas on what might be causing it? The arguments I pass to the console are -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);
    }

CodePudding user response:

In your ApplicationArguments class, you have public fields, not properties. Try making them auto-implemented properties (e.g. public int TenantId { get; set; } ) . Reading the error message, that is probably going to do the trick.

Also, that's what they have in the FluentCommandLineParser project's own Example: https://github.com/fclp/fluent-command-line-parser#usage

Citation:

public class ApplicationArguments
{
   public int RecordId { get; set; }
   public bool Silent { get; set; }
   public string NewValue { get; set; }
}
  •  Tags:  
  • c#
  • Related