Files
DP44/Common/DTS.Common.Serialization/IRIGCH10/Attributes/PacketHeaderValueAttribute.cs
2026-04-17 14:55:32 -04:00

83 lines
3.6 KiB
C#

using System;
using System.Linq;
namespace DTS.Serialization.IRIGCh10.Attributes
{
/// <summary>
/// allows annotating an enum with a value,
/// see TABLE 10-7. DATA TYPE NAMES AND DESCRIPTIONS in Chapter 10 pdf
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class PacketHeaderValueAttribute : Attribute
{
/// <summary>Specifies the default value for the <see cref="PacketHeaderValueAttribute" />, which is 0x00. This <see langword="static" /> field is read-only.</summary>
public static readonly PacketHeaderValueAttribute Default = new PacketHeaderValueAttribute();
private byte _packetHeader;
/// <summary>Initializes a new instance of the <see cref="PacketHeaderValueAttribute" /> class with no parameters.</summary>
public PacketHeaderValueAttribute()
: this(0x00)
{
}
/// <summary>Initializes a new instance of the <see cref="PacketHeaderValueAttribute" /> class with a PacketHeader.</summary>
/// <param name="packetHeader">packet header value. </param>
public PacketHeaderValueAttribute(byte packetHeader)
{
_packetHeader = packetHeader;
}
/// <summary>Gets the packet header value stored in this attribute.</summary>
/// <returns>The packet header value stored in this attribute.</returns>
public virtual byte PacketHeader => PacketHeaderValue;
/// <summary>Gets or sets the byte stored as the packet header value.</summary>
/// <returns>The byte stored as the packet header version value. The default value is 0x00.</returns>
protected byte PacketHeaderValue
{
get => _packetHeader;
set => _packetHeader = value;
}
/// <summary>Returns whether the value of the given object is equal to the current <see cref="PacketHeaderValueAttribute" />.</summary>
/// <param name="obj">The object to test the value equality of. </param>
/// <returns>
/// <see langword="true" /> if the value of the given object is equal to that of the current; otherwise, <see langword="false" />.</returns>
public override bool Equals(object obj)
{
if (obj == this)
return true;
if (obj is PacketHeaderValueAttribute packetHeaderAttribute)
return packetHeaderAttribute.PacketHeader == PacketHeader;
return false;
}
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return PacketHeader.GetHashCode();
}
/// <summary>Returns a value indicating whether this is the default <see cref="PacketHeaderValueAttribute" /> instance.</summary>
/// <returns>
/// <see langword="true" />, if this is the default <see cref="PacketHeaderValueAttribute" /> instance; otherwise, <see langword="false" />.</returns>
public override bool IsDefaultAttribute()
{
return Equals(Default);
}
public static byte GetPacketHeaderValue(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (PacketHeaderValueAttribute[])fi.GetCustomAttributes(
typeof(PacketHeaderValueAttribute), false);
if (attributes.Any())
{
return attributes.First().PacketHeaderValue;
}
return Default.PacketHeaderValue;
}
}
}