DateTime parameter results in AmbiguousMatchException

Sep 29, 2011 at 2:53 PM

I tried to parse a DateTime value into the following object:

   class MyCommandLineOption
   {
      [ValueArgument(typeof(DateTime), 'd', "date")]
      public DateTime time;
   }

and start the program with:
myprogram.exe -d "2008-11-01T19:35:00.0000000Z"

but this results in a System.Reflection.AmbiguousMatchException:

System.Reflection.AmbiguousMatchException wurde nicht behandelt.
  Message=Mehrdeutige Übereinstimmung gefunden.
  Source=mscorlib
  StackTrace:
       bei System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
       bei System.Type.GetMethod(String name)
       bei CommandLineParser.Arguments.ValueArgument`1.DefaultConvert(String stringValue)
       bei CommandLineParser.Arguments.ValueArgument`1.Convert(String stringValue)
       bei CommandLineParser.Arguments.ValueArgument`1.Parse(IList`1 args, Int32& i)
       bei CommandLineParser.CommandLineParser.ParseCommandLine(String[] args)
       bei CommandLineParsing.Program.CommandLineParserLibraryWithOptionClass(String[] args) in C:\Users\andre.erlinghagen\Desktop\CommandLineParsing\CommandLineParsing\CommandLineParsing\Program.cs:Zeile 55.
       bei CommandLineParsing.Program.Main(String[] args) in C:\Users\andre.erlinghagen\Desktop\CommandLineParsing\CommandLineParsing\CommandLineParsing\Program.cs:Zeile 28.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException:

because

MethodInfo mi = typeof(DateTime).GetMethod("Parse");

isn't distinct.

I worked around with the following classes:

public class DateTimeValueArgument : CommandLineParser.Arguments.ValueArgument<DateTime>
   {
      public DateTimeValueArgument(char shortName, string longName)
         : base(shortName, longName)
      {
         this.ConvertValueHandler = (x) => { return DateTime.Parse(x); };
      }
   }

   public class DateTimeValueArgumentAttribute : ArgumentAttribute
   {
      public DateTimeValueArgumentAttribute(char shortName)
         : base(typeof(DateTimeValueArgument), shortName)
      {
      }

      public DateTimeValueArgumentAttribute(char shortName, string longName)
         : base(typeof(DateTimeValueArgument), shortName, longName)
      {
      }
   }  

Is there an elegant way to do this?

Coordinator
Feb 21, 2016 at 10:09 PM
This issue has been addressed in v3.0.0.1 which you now can get from NuGet.
The project has been migrated to GitHub, please use it for tracking the project, getting information about new releases & reporting all potential issues.