using System; using System.Collections.Generic; namespace Cosmos.Build.Common { public abstract class PropertiesBase { protected Dictionary mPropTable = new Dictionary(); public event EventHandler PropertyChanging; public event EventHandler PropertyChanged; public bool IsDirty { get; private set; } public Dictionary GetProperties() { Dictionary clonedTable = new Dictionary(); foreach (KeyValuePair pair in mPropTable) { clonedTable.Add(pair.Key, pair.Value); } return clonedTable; } /// /// Gets array of project names which are project independent. /// public abstract string[] ProjectIndependentProperties { get; } public void Reset() { mPropTable.Clear(); IsDirty = false; } public void SetProperty(string name, string value) { PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(name)); mPropTable.TryGetValue(name, out var xOldValue); mPropTable[name] = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name, xOldValue, value)); IsDirty = true; } public void SetProperty(string name, Object value) { SetProperty(name, value.ToString()); } public string GetProperty(string name) { return GetProperty(name, string.Empty); } /// /// Get string value of the property. /// /// Name of the property. /// Default value for the property. /// Vaue of the property with given name. public string GetProperty(string name, string @default) { string value = @default; if (mPropTable.ContainsKey(name) == true) { value = mPropTable[name]; } return value; } /// /// Gets typed value of the property. /// /// Get property type. /// Get name of the property. /// Default value for the proeprty. /// Value of the property with given name. public T GetProperty(string name, T @default) where T : struct { T value = @default; if (mPropTable.ContainsKey(name) == true) { string stringValue = mPropTable[name]; Type valueType = typeof(T); string valueTypeName = valueType.Name; if (valueType.IsEnum) { value = EnumValue.Parse(stringValue, @default); } else { if ((valueTypeName == "Int16") || (valueTypeName == "Short")) { Int16 newValue; if (Int16.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if ((valueTypeName == "Int32") || (valueTypeName == "Integer")) { Int32 newValue; if (Int32.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if ((valueTypeName == "Int64") || (valueTypeName == "Long")) { Int32 newValue; if (Int32.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if (valueTypeName == "Boolean") { Boolean newValue; if (Boolean.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if ((valueTypeName == "UInt16") || (valueTypeName == "UShort")) { UInt16 newValue; if (UInt16.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if ((valueTypeName == "UInt32") || (valueTypeName == "UInteger")) { UInt32 newValue; if (UInt32.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else if ((valueTypeName == "UInt64") || (valueTypeName == "ULong")) { UInt64 newValue; if (UInt64.TryParse(stringValue, out newValue) == true) { value = (T)((Object)newValue); } } else { throw new ArgumentException("Unsupported value type.", "T"); } } } return value; } } }