I have a List
for Windows Performance Counters, which is static and cannot be changed at run-time. However, over the time this list has grown so large that it is extremely tedious to detect duplicates manually. For this I have written a tool which can run for checking the duplicates but still it is possible that people may forget to run that tool or are not aware of it.
I was to figure out if there is a way to detect the duplicates at compile time itself. The code looks something like this:
public enum SampleEnum
{
[Description("Sample")]
Sample,
}
And another class to define the Performance Counter:
public class PerfCounter
{
public int CounterType { get; set; }
public string CounterName { get; set; }
}
These two classes are used as follows:
public static class PerfCounterManager
{
public static List<PerfCounter> GetCounters()
{
return new List<PerfCounter>
{
new PerfCounter
{
CounterTypeId = (int) SampleEnum.Sample1,
CounterName = GetEnumDescription(CounterType.Sample1)
},
new PerfCounter
{
CounterTypeId = (int) SampleEnum.Sample2,
CounterName = GetEnumDescription(CounterType.Sample2)
},
}
}
}
The function GetCounters()
may be called many times, but will not be modified except during compilation.
The error can get thrown not only because CounterTypeId
can be duplicates, but also because CounterName
which is the description of the enum can be duplicate. And because the list is very large, it is not possible to detect this error while someone is adding elements to the list. It will be very useful if this can be detected at compile time itself.