You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The easiest way I can think of is to allow customTypeConverters 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.
publicclassCustomDateTimeOptions{[System.ComponentModel.TypeConverter(typeof(CustomDateTimeConverter))][Option('d',"date",Description="Store the date.")]publicDateTimeCurrentDate{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.
So a change you’re waiting on is in the develop branch?
I’m working to try to get a release out relatively quickly. Anything in that develop branch will be part of it.
E.Newton
On Sep 27, 2018, at 10:53 AM, Shmueli Englard ***@***.***> wrote:
@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
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
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().
Activity
ericnewton76 commentedon Nov 4, 2017
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?
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 fromtypeof(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
TypeConverterAttributesattached 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.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.
shmuelie commentedon Sep 27, 2018
Would a pull request to make
CommandLine.Core.TypeConverteruseSystem.ComponentModel.TypeConverterAttributeon a property be accepted?nemec commentedon Sep 27, 2018
Doesn't it already (see line 61)? What would you plan to change?
https://github.com/commandlineparser/commandline/blob/master/src/CommandLine/Core/TypeConverter.cs#L52-L63
shmuelie commentedon Sep 27, 2018
@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
ericnewton76 commentedon Sep 27, 2018
shmuelie commentedon Sep 27, 2018
Yup and great!
kvalev commentedon Aug 11, 2020
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().