/* Property.cs Copyright © 2008 Diversified Technical Systems, Inc. All Rights Reserved */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using DTS.Common.Utilities; namespace DTS.Common.Utilities.DotNetProgrammingConstructs { /// /// A class to implement self-checking, on-error-auto-syntax-building properties. /// /// /// /// The property type. /// /// public partial class Property : Exceptional { /// /// value of the property /// public Type Value { get { try { //if ( !_isValueInitialized ) // throw new NotInitializedException(Properties.Resources.Property_Value_NotInitializedString ); //else return _Value; } catch (System.Exception ex) { throw new Exception( string.Format( Properties.Resources.Property_Value_CouldNotGetValueString, !string.IsNullOrEmpty(_name) ? "\"" + _name + "\" " : ""), ex); } } set { try { if (!IsValidValue(value)) throw new InvalidValueException(GetInvalidValueDescription(value)); else { _Value = value; IsValueInitialized = true; } } catch (System.Exception ex) { throw new Exception( string.Format( Properties.Resources.Property_Value_CouldNotSetValueString, !string.IsNullOrEmpty(_name) ? "\"" + _name + "\" " : ""), ex); } } } private Type _Value; public bool IsValueInitialized { get; private set; } = false; private string _name; /// /// Determine whether or not the specified value is valid for this property. /// /// /// /// The value to be validity checked. /// /// /// /// true if the value is valid; false otherwise. /// /// public virtual bool IsValidValue(Type value) { return true; } /// /// Get initialization status for this property. /// public bool IsInitialized => IsValueInitialized; /// /// "Deinitialize" this property. /// public void UnInitialize() { IsValueInitialized = false; } /// /// Generate a user-readable explanation as to why the specified value is /// not valid for this property. /// /// /// /// The value to be described. /// /// /// /// A description explaining why the specified value /// is not valid for this property. /// /// public virtual string GetInvalidValueDescription(Type value) { try { return string.Format(Properties.Resources.Property_GetInvalidValueDescription_CouldNotSetToValueString, !string.IsNullOrEmpty(_name) ? "\"" + _name + "\"" : "", null != value ? ("\"" + value.ToString() + "\"") : Properties.Resources.Generic_NullIndicatorString); } catch (System.Exception ex) { throw new Exception(Properties.Resources.Property_GetInvalidValueDescription_GetDescriptionFailedString, ex); } } /// /// Initialize an instance of the Property class. /// /// /// /// The name of the property this implementation is standing in for, so that /// error messages will be more informative. If a null name is passed, /// /// /// /// The initial value of the property. Can be read if the property is almost /// marked as "initialized". /// /// /// /// A switch that determines whether or not the property /// is to be considered initialized immediately after instantiation. /// /// public Property(string name, Type initialValue, bool isInitialized) { try { _name = name; _Value = initialValue; IsValueInitialized = isInitialized; } catch (System.Exception ex) { throw new ConstructionException( string.Format( Properties.Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName), ex); } } /// /// Initialize an instance of the Property class. /// /// /// /// The initial value of the property. Can be read if the property is almost /// marked as "initialized". /// /// /// /// A switch that determines whether or not the property /// is to be considered initialized immediately after instantiation. /// /// public Property(Type initialValue, bool isInitialized) : this(null, initialValue, isInitialized) { } /// /// Generate user-readable string for this object. /// /// /// /// A representing the current state of the object. /// /// public override string ToString() { try { return _Value.ToString(); } catch (System.Exception) { return base.ToString(); } } } }