211 lines
6.7 KiB
C#
211 lines
6.7 KiB
C#
/*
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// A class to implement self-checking, on-error-auto-syntax-building properties.
|
|
/// </summary>
|
|
///
|
|
/// <typeparam name="Type">
|
|
/// The property type.
|
|
/// </typeparam>
|
|
///
|
|
public partial class Property<Type>
|
|
: Exceptional
|
|
{
|
|
/// <summary>
|
|
/// value of the property
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// Determine whether or not the specified value is valid for this property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="value">
|
|
/// The value to be validity checked.
|
|
/// </param>
|
|
///
|
|
/// <returns>
|
|
/// <see cref="bool"/> true if the value is valid; false otherwise.
|
|
/// </returns>
|
|
///
|
|
public virtual bool IsValidValue(Type value)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get <see cref="bool"/> initialization status for this property.
|
|
/// </summary>
|
|
public bool IsInitialized => IsValueInitialized;
|
|
|
|
/// <summary>
|
|
/// "Deinitialize" this property.
|
|
/// </summary>
|
|
public void UnInitialize()
|
|
{
|
|
IsValueInitialized = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate a user-readable explanation as to why the specified value is
|
|
/// not valid for this property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="value">
|
|
/// The value to be described.
|
|
/// </param>
|
|
///
|
|
/// <returns>
|
|
/// A <see cref="string"/> description explaining why the specified value
|
|
/// is not valid for this property.
|
|
/// </returns>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of the Property class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="name">
|
|
/// 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,
|
|
/// </param>
|
|
///
|
|
/// <param name="initialValue">
|
|
/// The initial value of the property. Can be read if the property is almost
|
|
/// marked as "initialized".
|
|
/// </param>
|
|
///
|
|
/// <param name="isInitialized">
|
|
/// A <see cref="bool"/> switch that determines whether or not the property
|
|
/// is to be considered initialized immediately after instantiation.
|
|
/// </param>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of the Property class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="initialValue">
|
|
/// The initial value of the property. Can be read if the property is almost
|
|
/// marked as "initialized".
|
|
/// </param>
|
|
///
|
|
/// <param name="isInitialized">
|
|
/// A <see cref="bool"/> switch that determines whether or not the property
|
|
/// is to be considered initialized immediately after instantiation.
|
|
/// </param>
|
|
///
|
|
public Property(Type initialValue, bool isInitialized)
|
|
: this(null, initialValue, isInitialized)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate user-readable string for this object.
|
|
/// </summary>
|
|
///
|
|
/// <returns>
|
|
/// A <see cref="string"/> representing the current state of the object.
|
|
/// </returns>
|
|
///
|
|
public override string ToString()
|
|
{
|
|
try { return _Value.ToString(); }
|
|
catch (System.Exception)
|
|
{
|
|
return base.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|