Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

System.InvalidCastException parsing FluentCommandLineParser arguments

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }

>Solution :

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; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading