1
//-----------------------------------------------------------------------
2
// <copyright file="Arguments.cs" company="JHOB Technologies, LLC">
3
// Copyright © JHOB Technologies, LLC. All rights reserved.
5
// <license>Microsoft Public License</license>
6
// <author>Brian Schloz</author>
7
//-----------------------------------------------------------------------
8
namespace SQLInstaller.Core
11
using System.Collections.Generic;
12
using System.Collections.ObjectModel;
13
using System.ComponentModel;
14
using System.ComponentModel.DataAnnotations;
17
public sealed class Arguments<T> where T : new()
19
private Collection<ValidationResult> validationResults;
21
public Arguments(IEnumerable<string> args, T instance)
23
this.OriginalArgs = args;
24
this.Instance = instance;
25
this.validationResults = new Collection<ValidationResult>();
30
public Arguments(IEnumerable<string> args)
35
public IEnumerable<string> OriginalArgs { get; private set; }
37
public T Instance { get; private set; }
41
get { return this.validationResults.Count == 0; }
44
public string ValidationErrors
48
StringBuilder error = new StringBuilder();
49
foreach (ValidationResult result in this.validationResults)
51
error.Append(result.ErrorMessage);
55
return error.ToString();
61
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this.Instance);
63
foreach (string arg in this.OriginalArgs)
65
if (arg.Length <= 1 || !arg.StartsWith("/", StringComparison.InvariantCulture))
71
string value = string.Empty;
73
int equalIdx = arg.IndexOf('=');
76
key = arg.Substring(1, equalIdx - 1);
77
value = arg.Substring(equalIdx + 1);
81
key = arg.Substring(1);
84
PropertyDescriptor property = this.FindProperty(key, properties);
88
if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?))
90
property.SetValue(this.Instance, true);
94
TypeConverter typeConverter = TypeDescriptor.GetConverter(property.PropertyType);
95
if (typeConverter != null && typeConverter.CanConvertFrom(typeof(string)))
97
property.SetValue(this.Instance, typeConverter.ConvertFromInvariantString(value));
103
Validator.TryValidateObject(this.Instance, new ValidationContext(this.Instance, null, null), this.validationResults, true);
107
/// Find an unambiguous matching property for the given argument.
109
/// <param name="keyName"></param>
110
/// <param name="properties"></param>
111
/// <returns></returns>
112
private PropertyDescriptor FindProperty(string keyName, PropertyDescriptorCollection properties)
114
List<PropertyDescriptor> matches = new List<PropertyDescriptor>();
115
foreach (PropertyDescriptor prop in properties)
117
// Ignore property hacks for xml serialization.
118
if (prop.Name.EndsWith("Specified", StringComparison.OrdinalIgnoreCase))
123
// An exact match on the argument/property will always succeed. Otherwise, do a partial match.
124
// E.g. Two arguments - Foo and FooBar. /Foo will succeed /FooB will succeed. /Fo or /F will fail (ambiguous).
125
if (string.Compare(prop.Name, keyName, StringComparison.OrdinalIgnoreCase) == 0)
131
else if (keyName.Length == 1)
133
if (prop.Name.StartsWith(keyName, StringComparison.OrdinalIgnoreCase))
138
else if (prop.Name.IndexOf(keyName, StringComparison.OrdinalIgnoreCase) >= 0)
144
if (matches.Count == 1)