using System;
using System.Linq;
namespace DTS.Serialization.IRIGCh10.Attributes
{
///
/// this class is used to annotate enums with a version attribute
/// for example each data packet type has a corresponding version number
/// example TABLE 10-7. DATA TYPE NAMES AND DESCRIPTIONS in chapter 10.pdf
///
[AttributeUsage(AttributeTargets.All)]
public class DataTypeVersionValueAttribute : Attribute
{
/// Specifies the default value for the , which is 0x00. This field is read-only.
public static readonly DataTypeVersionValueAttribute Default = new DataTypeVersionValueAttribute();
private byte _dataTypeVersion;
/// Initializes a new instance of the class with no parameters.
public DataTypeVersionValueAttribute()
: this(0x00)
{
}
/// Initializes a new instance of the class with a DataTypeVersionValue.
/// The data type version.
public DataTypeVersionValueAttribute(byte dataTypeVersion)
{
_dataTypeVersion = dataTypeVersion;
}
/// Gets the data type version stored in this attribute.
/// The data type version stored in this attribute.
public virtual byte DataTypeVersion => DataTypeVersionValue;
/// Gets or sets the byte stored as the datatype version.
/// The byte stored as the data type version. The default value is 0x00.
protected byte DataTypeVersionValue
{
get => _dataTypeVersion;
set => _dataTypeVersion = value;
}
/// Returns whether the value of the given object is equal to the current .
/// The object to test the value equality of.
///
/// if the value of the given object is equal to that of the current; otherwise, .
public override bool Equals(object obj)
{
if (obj == this)
return true;
if (obj is DataTypeVersionValueAttribute attribute)
return attribute.DataTypeVersion == DataTypeVersion;
return false;
}
/// Returns the hash code for this instance.
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return DataTypeVersion.GetHashCode();
}
/// Returns a value indicating whether this is the default instance.
///
/// , if this is the default instance; otherwise, .
public override bool IsDefaultAttribute()
{
return Equals(Default);
}
public static byte GetDataTypeVersionValue(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (DataTypeVersionValueAttribute[])fi.GetCustomAttributes(
typeof(DataTypeVersionValueAttribute), false);
if (attributes.Any())
{
return attributes.First().DataTypeVersionValue;
}
return Default.DataTypeVersionValue;
}
}
}