Implement new functionality suggested in Issue 283 by e673 · Pull Request #286 · commandlineparser/commandline
Expand Up
@@ -80,51 +80,30 @@ public static TargetType ToTargetType(this Type type)
: TargetType.Scalar;
}
public static T SetProperties<T>( public static IEnumerable<Error> SetProperties<T>( this T instance, IEnumerable<SpecificationProperty> specProps, Func<SpecificationProperty, bool> predicate, Func<SpecificationProperty, object> selector) { return specProps.Where(predicate).Aggregate( instance, (current, specProp) => { specProp.Property.SetValue(current, selector(specProp)); return instance; }); return specProps.Where(predicate).SelectMany(specProp => specProp.SetValue(instance, selector(specProp))); }
private static T SetValue<T>(this PropertyInfo property, T instance, object value) private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value) { Action<Exception> fail = inner => { throw new InvalidOperationException("Cannot set value to target instance.", inner); };
try { property.SetValue(instance, value, null); } #if !PLATFORM_DOTNET catch (TargetException e) { fail(e); specProp.Property.SetValue(instance, value, null); return Enumerable.Empty<Error>(); } #endif catch (TargetParameterCountException e) { fail(e); } catch (MethodAccessException e) catch (TargetInvocationException e) { fail(e); return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) }; } catch (TargetInvocationException e) catch (Exception e) { fail(e); return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; }
return instance; }
public static object CreateEmptyArray(this Type type) Expand Down
public static T SetProperties<T>( public static IEnumerable<Error> SetProperties<T>( this T instance, IEnumerable<SpecificationProperty> specProps, Func<SpecificationProperty, bool> predicate, Func<SpecificationProperty, object> selector) { return specProps.Where(predicate).Aggregate( instance, (current, specProp) => { specProp.Property.SetValue(current, selector(specProp)); return instance; }); return specProps.Where(predicate).SelectMany(specProp => specProp.SetValue(instance, selector(specProp))); }
private static T SetValue<T>(this PropertyInfo property, T instance, object value) private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value) { Action<Exception> fail = inner => { throw new InvalidOperationException("Cannot set value to target instance.", inner); };
try { property.SetValue(instance, value, null); } #if !PLATFORM_DOTNET catch (TargetException e) { fail(e); specProp.Property.SetValue(instance, value, null); return Enumerable.Empty<Error>(); } #endif catch (TargetParameterCountException e) { fail(e); } catch (MethodAccessException e) catch (TargetInvocationException e) { fail(e); return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) }; } catch (TargetInvocationException e) catch (Exception e) { fail(e); return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; }
return instance; }
public static object CreateEmptyArray(this Type type) Expand Down