Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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#_Namespaces - Fatal编程技术网

C# 比较名称空间以查看它们是否匹配

C# 比较名称空间以查看它们是否匹配,c#,namespaces,C#,Namespaces,我试图比较名称空间,看看我的方法是否只抛出正确的异常。除正确的例外情况外,我的意思是: 来自同一命名空间的异常 来自更高命名空间的异常 来自等效(及更高)系统命名空间的异常 示例: 方法位于命名空间MyNamespace.Collections.Generic中,因此它可以从MyNamespace.Collections.Generic引发异常 方法位于命名空间MyNamespace.Collections.Generic中,因此它可以从MyNamespace.Collections和MyNam

我试图比较名称空间,看看我的方法是否只抛出正确的异常。除正确的例外情况外,我的意思是:

  • 来自同一命名空间的异常
  • 来自更高命名空间的异常
  • 来自等效(及更高)系统命名空间的异常
  • 示例:

  • 方法位于命名空间
    MyNamespace.Collections.Generic
    中,因此它可以从
    MyNamespace.Collections.Generic
    引发异常
  • 方法位于命名空间
    MyNamespace.Collections.Generic
    中,因此它可以从
    MyNamespace.Collections
    MyNamespace
    引发异常
  • 方法位于命名空间
    MyNamespace.Collections.Generic
    中,因此它可以从
    System.Collections.Generic
    System.Collections
    引发异常
  • 第一部分很简单;正在检查相同的命名空间。3号的一部分也工作了;因为
    System
    名称空间总是正确的

    对于其他部分,我尝试了以下方法:

    string[] exceptNamespaceSegments = exceptionNamespaceSegments
                                       .Except(classNamespaceSegments)
                                       .ToArray();
    
    if (exceptNamespaceSegments.Length == 1 && exceptNamespaceSegments.FirstOrDefault() == "System")
        return;
    
    检查命名空间段(集合,泛型)是否也在类命名空间中使用。如果是这种情况,将抛出正确的异常

    但是,如果异常出现在命名空间
    System.Collections.Generic.Something
    中,则这不适用,因为
    Something
    不在类命名空间中

    想想看,这并没有考虑到顺序。所以
    System.Generic.Collections
    也是正确的;使用我目前拥有的


    是否有任何方法可以在不必编写大量if语句的情况下完成这项工作,以比较每个单独的段?

    除非我误解了这个问题:您可以尝试这样的方法,根据给定的标准查找所有允许的名称空间

    private static IEnumerable<string> GetAllowedNamespaces(Type type)
    {
        const string System = "System";
        string thisNamespace = type.Namespace;
        HashSet<string> hashset = new HashSet<string>();
        hashset.Add(thisNamespace);
        hashset.Add(System);
        var parts = thisNamespace.Split('.');
        if (parts.Length > 1)
        {
            string previous = string.Empty;
            foreach (var part in parts)
            {
                var current = string.IsNullOrEmpty(previous)
                    ? part
                    : string.Format("{0}.{1}", previous, part);
                previous = current;
                hashset.Add(current);
            }
    
            previous = string.Empty;
            foreach (var part in new[] { System  }.Concat(parts.Skip(1)))
            {
                var current = string.IsNullOrEmpty(previous)
                    ? part
                    : string.Format("{0}.{1}", previous, part);
                previous = current;
                hashset.Add(current);
            }
        }
    
        return hashset;
    }
    
    私有静态IEnumerable GetAllowedNamespaces(类型)
    {
    常量字符串System=“System”;
    string thisNamespace=type.Namespace;
    HashSet HashSet=新的HashSet();
    Add(thisNamespace);
    添加(系统);
    var parts=thisNamespace.Split('.');
    如果(零件长度>1)
    {
    string previous=string.Empty;
    foreach(var零件中的零件)
    {
    var current=string.IsNullOrEmpty(上一个)
    部分
    :string.Format(“{0}.{1}”,前一部分);
    先前=当前;
    hashset.Add(当前);
    }
    previous=string.Empty;
    foreach(新[]{System}.Concat(parts.Skip(1))中的var部分)
    {
    var current=string.IsNullOrEmpty(上一个)
    部分
    :string.Format(“{0}.{1}”,前一部分);
    先前=当前;
    hashset.Add(当前);
    }
    }
    返回哈希集;
    }
    
    然后,您可以轻松地检查异常的命名空间是否属于此列表,如果没有问题:)


    存在重复的代码块,您可以将其重构为一种遵循DRY原则的方法。

    好奇:为什么要这样做?从另一个名称空间抛出异常有什么不对?我们研究的一个案例是,类
    列表
    抛出了
    FileNotFoundException
    ,这显然是不应该发生的。所以我们决定了这个规则,除了一些例外(哈哈)@SriramSakthivel我实际上是在5分钟前写的!抱歉:(List不会抛出FileNotFoundException(除了关于序列化的问题)。可能List中存储的对象会抛出异常。但是,检查哪些异常可以通过List的方法抛出是错误的…@Matthijs这很好,但我在15分钟前回答了..我删除了我的答案,如果您感兴趣,将取消删除...)我知道你从哪里来,但我认为这是一种过于复杂的事情。实际上编写了一个似乎工作正常的解决方案,请参见此处: