Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在编译时检查列表中的重复项_C#_.net_List_Unique - Fatal编程技术网

C# 如何在编译时检查列表中的重复项

C# 如何在编译时检查列表中的重复项,c#,.net,list,unique,C#,.net,List,Unique,我有一个用于Windows性能计数器的列表,它是静态的,不能在运行时更改。然而,随着时间的推移,这个列表变得如此之大,以至于手动检测重复项是极其繁琐的。为此,我写了一个工具,可以运行检查副本,但人们可能忘记运行该工具或不知道它 我想知道是否有一种方法可以在编译时检测重复项。代码如下所示: public enum SampleEnum { [Description("Sample")] Sample, } 以及定义性能计数器的另一个类: public class PerfCoun

我有一个用于Windows性能计数器的
列表
,它是静态的,不能在运行时更改。然而,随着时间的推移,这个列表变得如此之大,以至于手动检测重复项是极其繁琐的。为此,我写了一个工具,可以运行检查副本,但人们可能忘记运行该工具或不知道它

我想知道是否有一种方法可以在编译时检测重复项。代码如下所示:

public enum SampleEnum
{
    [Description("Sample")]
    Sample,
}
以及定义性能计数器的另一个类:

public class PerfCounter
{
    public int CounterType { get; set; }
    public string CounterName { get; set; }
}
这两个类别的用途如下:

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)
            },
        }
    }
}
公共静态类管理器
{
公共静态列表GetCounters()
{
返回新列表
{
新性能计数器
{
CounterTypeId=(int)SampleEnum.Sample1,
CounterName=GetEnumDescription(CounterType.Sample1)
},
新性能计数器
{
CounterTypeId=(int)SampleEnum.Sample2,
CounterName=GetEnumDescription(CounterType.Sample2)
},
}
}
}
函数
GetCounters()
可能会被多次调用,但除非在编译过程中,否则不会被修改


引发此错误的原因不仅是
CounterTypeId
可以重复,还因为
CounterName
是枚举的描述,可以重复。由于列表非常大,因此在有人向列表中添加元素时不可能检测到此错误。如果能够在编译时检测到这一点,这将是非常有用的。

除了一些约定之外,在编译时可以做的事情不多。看起来您已经有了值的枚举-不使用类似于
Sample44=12的东西将防止重复的枚举值。一些合理的描述命名方案有助于描述的重复

安全的解决方案是添加单元测试,只需检查该条件——每个人都运行单元测试,并立即发现问题。(如果您没有单元测试-这是开始使用它们的好借口)。

使用
哈希集
排序集
作为
列表
的后备存储。类似于以下内容,不过您可能需要合适的IEqualityComparer来正确定义您的独特性概念。这不会通过抛出编译时错误来强制唯一性,但它将保证唯一性。可能是一个有用的扩展,用于修改操作,并记录有关缺少唯一性的消息,用于诊断目的……这样您就可以追踪犯罪方并对其进行合理的打击:^)

公共静态类管理器
{
公共静态列表GetCounters()
{
返回新列表
{
新分类集
{
新性能计数器
{
CounterTypeId=(int)SampleEnum.Sample1,
CounterName=GetEnumDescription(CounterType.Sample1)
},
新性能计数器
{
CounterTypeId=(int)SampleEnum.Sample2,
CounterName=GetEnumDescription(CounterType.Sample2)
},
}
}
}
}

[顺便说一句,虽然我喜欢我的iPad,因为它写代码,但这是我用过的最糟糕的东西。]

可能是重复的感谢链接,我会看一下。在构建后事件上运行你的工具,或者使用T4生成你的代码,在那里你可以有各种自定义逻辑和验证以及LINQ等等。正如我所说的,我已经有了一个工具来检查Windows性能计数器是否已成功创建,但人们(包括我自己)忘记多次运行该工具-这就是为什么在编译时检测重复项并生成编译器警告将非常有用的原因。@babbupandey,为什么不简单地运行“工具”作为构建的一部分,如果您没有/不想要单元测试?
public static class PerfCounterManager
{
  public static List<PerfCounter> GetCounters() 
  {
    return new List<PerfCounter> 
    {
      new SortedSet<PerfCounter>
      {
        new PerfCounter
        {
          CounterTypeId = (int) SampleEnum.Sample1,
          CounterName = GetEnumDescription(CounterType.Sample1)
        },
        new PerfCounter
        {
          CounterTypeId = (int) SampleEnum.Sample2,
          CounterName = GetEnumDescription(CounterType.Sample2)
        },
      }
    }
  }
}