Skip to content

Commit

Permalink
use the JSON deserialization exception when throwing a TypeConvertion…
Browse files Browse the repository at this point in the history
…Exception
  • Loading branch information
adrianaisemberg committed Oct 24, 2011
1 parent 4119780 commit ebf4118
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 42 deletions.
82 changes: 42 additions & 40 deletions CLAP/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,34 +630,43 @@ private object GetValueForParameter(Type parameterType, string inputKey, string
{
return Serialization.Deserialize(stringValue, parameterType);
}
catch // can't deserialize - try converting
catch (Exception ex)
{
// if array
if (parameterType.IsArray)
// can't deserialize - try converting
//
try
{
var stringValues = stringValue.CommaSplit();
// if array
if (parameterType.IsArray)
{
var stringValues = stringValue.CommaSplit();

// The type of the array element
//
var type = parameterType.GetElementType();
// The type of the array element
//
var type = parameterType.GetElementType();

// Create a generic instance of the ConvertToArray method
//
var convertToArrayMethod = GetType().GetMethod(
"ConvertToArray",
BindingFlags.NonPublic | BindingFlags.Static).
MakeGenericMethod(type);
// Create a generic instance of the ConvertToArray method
//
var convertToArrayMethod = GetType().GetMethod(
"ConvertToArray",
BindingFlags.NonPublic | BindingFlags.Static).
MakeGenericMethod(type);

// Run the array converter
//
return convertToArrayMethod.Invoke(null, new[] { stringValues });
// Run the array converter
//
return convertToArrayMethod.Invoke(null, new[] { stringValues });
}
// if there is an input value
else if (stringValue != null)
{
// convert the string value to the relevant parameter type
//
return ConvertString(stringValue, parameterType);
}
}
// if there is an input value
else if (stringValue != null)
catch // use the JSON exception
{
// convert the string value to the relevant parameter type
//
return ConvertString(stringValue, parameterType);
throw new TypeConvertionException(stringValue, parameterType, ex);
}
}
}
Expand All @@ -678,28 +687,21 @@ private static TConvert[] ConvertToArray<TConvert>(string[] values)
/// </summary>
private static object ConvertString(string value, Type type)
{
try
if (type.IsEnum)
{
if (type.IsEnum)
{
return Enum.Parse(type, value);
}
else if (type == typeof(Guid))
{
return string.IsNullOrEmpty(value) ? (object)null : new Guid(value);
}
else if (type == typeof(Uri))
{
return string.IsNullOrEmpty(value) ? (object)null : new Uri(Environment.ExpandEnvironmentVariables(value));
}
else
{
return Convert.ChangeType(value, type);
}
return Enum.Parse(type, value);
}
catch (Exception ex)
else if (type == typeof(Guid))
{
return string.IsNullOrEmpty(value) ? (object)null : new Guid(value);
}
else if (type == typeof(Uri))
{
return string.IsNullOrEmpty(value) ? (object)null : new Uri(Environment.ExpandEnvironmentVariables(value));
}
else
{
throw new TypeConvertionException(value, type, ex);
return Convert.ChangeType(value, type);
}
}

Expand Down
37 changes: 35 additions & 2 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using CLAP;
using CLAP.Validation;
using System.Threading;
using System.Linq;

namespace ConsoleTest
{
Expand Down Expand Up @@ -51,6 +52,22 @@ public static void Help(string h)
public static void Pong(int num)
{
}

[Global(Aliases = "d")]
public static void Debug()
{
Debugger.Launch();
}

[Verb(Aliases = "save")]
public static void SavePerson(Person p)
{
}

[Verb]
public static void SavePersons(IEnumerable<Person> p)
{
}
}

class BaseApp
Expand Down Expand Up @@ -622,5 +639,21 @@ public void Validate(ParameterInfoAndValue[] parameters)
}
}
}



public class Person
{
public int Age { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}

public class PhoneNumber
{
public string Type { get; set; }
public string Number { get; set; }
}
}

0 comments on commit ebf4118

Please sign in to comment.