Right now I have this interface called IMessageConfiguration<T>
that implements a property called Label
(of type byte
). I have a map of labels to their relative message configurations in the following structure:
Dictionary<byte, dynamic> configurationMap = new Dictionary<byte, dynamic>();
Now, although the dictionary is dynamic I am only filling it with either IMessageConfiguration<double>
or IMessageConfiguration<uint>
. Regardless of which type is at each dictionary entry, the entry will have the property Label
because all entries will ultimately have the IMessageConfiguration<T>
base type. I do however understand that the compiler will have no knowledge of this.
I am writing a routine to go through the dictionary and obtain the Label
property for each of the IMessageConfiguration<T>
entries. Here is my current attempt at doing this:
public IList<byte> GetLabels()
{
IList<byte> labels = new List<byte>();
// obtain all of the labels
foreach (var configuration in this.configurationMap)
{
if (configuration.Value is IMessageConfiguration<double>)
{
labels.Add((configuration.Value as IMessageConfiguration<double>).Label);
}
else if (configuration.Value is IMessageConfiguration<uint>)
{
labels.Add((configuration.Value as IMessageConfiguration<uint>).Label);
}
}
return labels;
}
Is there a way to more cleanly obtain all of the Label
properties for each of the message configurations?
As asked in the comments, here is the full definition for the IMessageConfiguration<T>
interface.
public interface IMessageConfiguration<T>
{
string Description { get; }
byte Label { get; }
ushort LSB { get; }
ushort MSB { get; }
double Resolution { get; }
int SignBit { get; }
string Title { get; }
string Units { get; }
uint Encode(T data);
T Decode(uint message);
}