public void Finish(object destination)
{
if (IsRequired && !SeenValue) {
throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture, "Missing required argument '-{0}'.", LongName));
}
if (IsArray) {
_propertyInfo.SetValue(destination, _collectionValues.ToArray(_elementType), BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
} else if (IsCollection) {
// If value of property is null, create new instance of collection
if (_propertyInfo.GetValue(destination, BindingFlags.Default, null, null, CultureInfo.InvariantCulture) == null) {
if (!_propertyInfo.CanWrite) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1171")
+ " but is not initialized and does not allow the"
+ "collection to be initialized.", LongName));
}
object instance = Activator.CreateInstance(_propertyInfo.PropertyType, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture);
_propertyInfo.SetValue(destination, instance, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
}
object value = _propertyInfo.GetValue(destination,
BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
MethodInfo addMethod = null;
// Locate Add method with 1 parameter
foreach (MethodInfo method in value.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance)) {
if (method.Name == "Add" && method.GetParameters().Length == 1) {
ParameterInfo parameter = method.GetParameters()[0];
if (parameter.ParameterType != typeof(object)) {
addMethod = method;
break;
}
}
}
if (addMethod == null) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1169"), LongName));
} else {
try {
foreach (object item in _collectionValues) {
addMethod.Invoke(value, BindingFlags.Default, null, new object[] {item}, CultureInfo.InvariantCulture);
}
} catch (Exception ex) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1173"),
LongName), ex);
}
}
} else if (IsNameValueCollection) {
// If value of property is null, create new instance of collection
if (_propertyInfo.GetValue(destination, BindingFlags.Default, null, null, CultureInfo.InvariantCulture) == null) {
if (!_propertyInfo.CanWrite) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1171")
+ " but is not initialized and does not allow the"
+ "collection to be initialized.", LongName));
}
object instance = Activator.CreateInstance(_propertyInfo.PropertyType, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture);
_propertyInfo.SetValue(destination, instance, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
}
object value = _propertyInfo.GetValue(destination,
BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
MethodInfo addMethod = null;
// Locate Add method with 2 string parameters
foreach (MethodInfo method in value.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance)) {
if (method.Name == "Add" && method.GetParameters().Length == 2) {
if (method.GetParameters()[0].ParameterType == typeof(string) &&
method.GetParameters()[1].ParameterType == typeof(string)) {
addMethod = method;
break;
}
}
}
if (addMethod == null) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1169"), LongName));
} else {
try {
foreach (string key in _valuePairs) {
addMethod.Invoke(value, BindingFlags.Default, null,
new object[] {key, _valuePairs.Get(key)},
CultureInfo.InvariantCulture);
}
} catch (Exception ex) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1173"),
LongName), ex);
}
}
} else {
// this fails on mono if the _argumentValue is null
if (_argumentValue != null) {
_propertyInfo.SetValue(destination, _argumentValue, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
}
}
}