I have a class containing string constants and a class containing readonly objects representing a counter object. I have created a custom attribute that tags the string constant with the string representation of it's corresponding counter. Is there a good way to link the string const to the counter object using the attribute?
Here is an example of the string const:
public static class OperatorDiagnosticsConstants
{
[CounterType(CounterType = "ReagentProbe1")]
public const string R1_PROBE_CODE = "SACT-158";
}
Here is the class containing the readonly counter objects:
public class MaintenanceCounterType : CounterTypeEnum
{
public static readonly MaintenanceCounterType ReagentProbe1 = new MaintenanceCounterType(MaintenanceCounterTypeConstants.ReagentProbe1ID);
}
I can think of two solutions but wonder if there is a more elegant way? First is in the code that uses these two classes, I could have a convert method with a switch statement. Switch on the attribute string to return the MaintenanceCounterType
public MaintenanceCounterType Convert(string attributeStr)
{
switch (attributeStr)
{
case "ReagentProbe1":
return MaintenanceCounterType.ReagentProbe1;
......
}
}
Or I think I could add the same custom attribute CounterType
to the MaintenanceCounterType
and use reflection to match them up. I guess by checking the equality of the string property of the custom attribute?
Looking for a more elegant solution. Thanks!
Aucun commentaire:
Enregistrer un commentaire