Skip to content

Ability to use converters and a type factory #83

Closed

Description

@ericnewton76

Issue by robsonfelix
Tuesday Aug 16, 2016 at 04:36 GMT
Originally opened as gsscoder/commandline#343


Giacomo, your library rocks!

I am trying to accommodate some needs we have for a project using the library and I need your help. We successfully added a way to use custom types (classes) as Options property types by changing the TypeConverter class on the MakeType function delegate by adding a try/catch with the constructor. That way we redirected the constructor to some other static factory method that was able to create the value instance.

The other thing we did was to change the ReflectionExtensions class on the SetValue method by adding a way to get the TypeDescriptor for a particular property once the original property.SetValue method failed on setting the property value.

However it looks like adding a Type Factory to the ParserSettings (instead of hardcoding such factory into the TypeConverter class) is too complicated. That would be the ideal scenario.

Is there anything you suggest we do to make this change easier?

We believe the changes we did are very good for other developers to use (we created a sort of type-variant class set that can work interchangeably with boolean, integer and other numeric types, text, etc). Hence the need to use other types (rather than struct or base .NET CLR types). As for the changes we did to your code, they can be gladly shared.

Activity

  1. ericnewton76 commented on Nov 4, 2017

    @ericnewton76
    MemberAuthor

    Comment by nemec
    Tuesday Aug 23, 2016 at 21:05 GMT


    Hi @robsonfelix, if I understand you correctly, you're trying to add the ability for Options/Values to be custom classes with no default constructor?

    class Options
    {
        [Option('n')]
        public MyClass Nothing { get; set; }
    }
    
    class MyClass
    {
        public MyClass(int counter)
        {
        }
    }

    The easiest way I can think of is to allow custom TypeConverters to be created and applied to the property. Then you simply check that the converter can convert from typeof(string) and let the converter create the new instance (along with populating its data from the option value).

    Within the code, you'd have to use reflection to get any TypeConverterAttributes attached to the property (not the property Type itself, though you can do that too) and use them to convert the "string value" into a real type.

    public class CustomDateTimeOptions
    {
        [System.ComponentModel.TypeConverter(typeof(CustomDateTimeConverter))]
        [Option('d', "date", Description = "Store the date.")]
        public DateTime CurrentDate { get; set; }
    }

    You can, of course, do what you're suggesting with an entry in the parser settings, but you'll have to make sure those factories are passed into the inner parts of the code where conversion happens.

  2. shmuelie commented on Sep 27, 2018

    @shmuelie

    Would a pull request to make CommandLine.Core.TypeConverter use System.ComponentModel.TypeConverterAttribute on a property be accepted?

  3. nemec commented on Sep 27, 2018

    @nemec
    Contributor
  4. shmuelie commented on Sep 27, 2018

    @shmuelie

    @nemec Ah, yes it does, just not in latest release so I hadn't realized. That would solve my issues perfectly then! Guess I'll wait! Thanks

  5. ericnewton76 commented on Sep 27, 2018

    @ericnewton76
    MemberAuthor
  6. shmuelie commented on Sep 27, 2018

    @shmuelie

    Yup and great!

  7. kvalev commented on Aug 11, 2020

    @kvalev

    I dont think this works in the latest version. I tried annotating my option's property, but the converter is not invoked. Instead I had to register it using TypeDescriptor.AddAttributes().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions