/*
AttributeCoder.cs
* 12/18/2008 Relo'd from Dave to SVN-controlled utilities project.
$Log: AttributeCoder.cs,v $
Revision 1.1 2006/09/14 18:41:54 Paul Hrissikopoulos
Moved AttributeCoder files from Dave solution.
Revision 1.3 2006/09/07 20:43:08 Paul Hrissikopoulos
Expanded commentary.
Revision 1.2 2006/08/17 23:44:46 Paul Hrissikopoulos
Removed freeloading DCX XML prototype code.
Revision 1.1 2006/08/17 23:05:11 Paul Hrissikopoulos
Added generically-derived SelectionCriterion.ComparisonType enumeration attributes.
Copyright © 2006
Diversified Technical Systems
All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
using DTS.Common.Utilities.Properties;
namespace DTS.Common.Utilities
{
///
/// Object for manipulating object-attached attributes and attribute values.
///
///
///
/// The type of the attributes we are concerned with are
/// are attached to.
///
///
///
/// The type of attribute to be manipulated.
///
///
///
/// The type of value contained by the attribute to be manipulated.
///
///
///
/// This class was created to make it easier to "match" attributes and the data
/// types they're attached to in a somewhat generic manner. It is intended that
/// attribute coder classes that deal with specific attribute types be derived
/// from this one.
///
///
public class AttributeCoder : Exceptional
{
///
/// Method that specifies how to access the value of a given "AttributeType".
///
///
///
/// The "AttributeType" attribute to have its value extracted.
///
///
///
/// The "AttributeValueType" value of the specified attribute.
///
///
public delegate AttributeValueType AttributeValueExtractionMethod(AttributeType attribute);
///
/// Method for determining the equality of two "AttributeValueType" values.
///
///
///
/// A "AttributeValueType" to be equality-compared.
///
///
///
/// A "AttributeValueType" to be equality-compared.
///
///
///
/// True if the two values are equal; false otherwise.
///
///
public delegate bool AttributeValueEqualityComparisonMethod(AttributeValueType value1, AttributeValueType value2);
private readonly AttributeValueExtractionMethod _extractAttributeValue;
private readonly AttributeValueEqualityComparisonMethod _areAttributeValuesEqual;
///
/// Initializes an instance of the AttributeCoder class.
///
///
///
/// A that defines how "AttributeValueType"
/// values are to be extracted from "AttributeType" attributes.
///
///
///
/// Optional parameter for specifying a custom comparison method to override default
/// "AttributeValueType" equality determination. Pass null to use default
/// comparison.
///
///
public AttributeCoder(AttributeValueExtractionMethod attributeValueExtractionMethod,
AttributeValueEqualityComparisonMethod attributeValueEqualityComparisonMethod)
{
try
{
if (null == attributeValueExtractionMethod)
throw new Exception(Resources.AttributeCoder_AttributeCoder_NullAttributeValueExtractionMethodReferenceString);
_extractAttributeValue = attributeValueExtractionMethod;
if (null != attributeValueEqualityComparisonMethod)
_areAttributeValuesEqual = attributeValueEqualityComparisonMethod;
}
catch (System.Exception ex)
{
throw new Exception(
string.Format(Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName),
ex);
}
}
///
/// Return the "AttributeValueType" of the "AttributeType" attribute
/// attached to the specified "TargetType".
///
///
///
/// The "TargetType" object whose "AttributeType" attribute is to be
/// evaluated.
///
///
///
/// The "AttributeValueType" of the "AttributeType" attribute attached
/// to the specified "TargetType".
///
///
public AttributeValueType DecodeAttributeValue(TargetType target)
{
try
{
var attributes = DecodeAttributeValues(target);
if (attributes.Count > 0) return attributes[0];
throw new Exception(Resources.AttributeCoder_NoTypeAttributesFoundOnTargetString);
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DecodeAttributeExceptionString, ex);
}
}
///
/// Return the of "AttributeValueType"s of the
/// "AttributeType" attached to the specified "TargetType".
///
///
///
/// The "TargetType" object whose "AttributeType" attribute is to be
/// evaluated.
///
///
///
/// The "AttributeValueType" of the "AttributeType" attribute attached
/// to the specified "TargetType".
///
///
public List DecodeAttributeValues(TargetType target)
{
try
{
var info = target.GetType().GetField(target.ToString());
var attributes = (info.GetCustomAttributes(typeof(AttributeType), false) as AttributeType[]);
var attributeList = new List();
if (attributes != null)
{
attributeList.AddRange(attributes.Select(t => _extractAttributeValue(t)));
}
return attributeList;
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DecodeAttributesExceptionString, ex);
}
}
///
/// Return the "TargetType" value that has the "AttributeType"
/// attribute with the specified "AttributeValueType" value attached
/// to it.
///
///
///
/// The value of the "AttributeType" attribute attached to the
/// "TargetType" target object we're looking for.
///
///
///
/// The "TargetType" target object that has the "AttributeType"
/// attribute attached to it that has a "AttributeValueType" value of
/// attributeValue.
///
///
public TargetType EncodeAttributeValue(AttributeValueType attributeValue)
{
try
{
var targets = DehashAttributeValue(attributeValue);
if (1 != targets.Count) throw new Exception(Resources.AttributeCoder_UnableToFindTargetTypeMappingString);
return targets[0];
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_EncodeAttributeExceptionString, ex);
}
}
///
/// Return a list of "TargetType" values that have the
/// "AttributeType" attribute with the specified
/// "AttributeValueType" value attached to them.
///
///
///
/// The value of the "AttributeType" attribute attached to the
/// "TargetType" target object we're looking for.
///
///
///
/// The of "TargetType" target objects that has
/// the "AttributeType" attribute attached to it that has a
/// "AttributeValueType" value of attributeValue.
///
///
public List DehashAttributeValue(AttributeValueType attributeValue)
{
try
{
var targets = Enum.GetValues(typeof(TargetType)).Cast()
.Where(target => (null != _areAttributeValuesEqual) ?
_areAttributeValuesEqual(DecodeAttributeValue(target), attributeValue) : DecodeAttributeValue(target).Equals(attributeValue)).ToList();
if (targets.Count <= 0) throw new Exception(Resources.AttributeCoder_DehashAttributeValue_UnableToMatchAttributeValueWithTargetString);
return targets;
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DehashAttributeValue_DehashAttributeValueExceptionString, ex);
}
}
}
}