简体   繁体   English

嵌套类没有命名空间

[英]Nested classes don't have a namespace

I am writing a custom FxCop-rule which checks if Exceptionclasses inherit from the correct Exception-namespaces.我正在编写一个自定义 FxCop 规则,它检查异常类是否从正确的异常命名空间继承。 This check itself works perfectly.这个检查本身工作得很好。 But, in my testcase, I came across something weird.但是,在我的测试用例中,我遇到了一些奇怪的事情。

namespace MyNamespace.IO
{
    [TestClass]
    public class ExceptionsShouldOnlyInheritFromCorrectNamespacesTests
    {
        [TestMethod]
        public void TestExceptionInheritanceNamespace()
        {
            RuleAssert.AssertProblemsAreEqualToExpectedProblems<ExceptionsShouldOnlyInheritFromCorrectNamespaces, ExceptionInhertTestClass>();
        }    
    }

    namespace Something
    {
        [CaRuleTestClass]
        public class ExceptionInhertTestClass
        {
            public class MyFirstException : SystemException
            {                   
            }

            public class MySecondException : FileNotFoundException
            {                   
            }

            [ExpectedProblem]
            public class MyWrongException : AbandonedMutexException
            {                   
            }
        }
    }
}

The above code demonstrates the setup.上面的代码演示了设置。 As you can see I have a nested-namespace (this is only for testpurposes).如您所见,我有一个嵌套命名空间(这仅用于测试目的)。 In this there is one main class ExceptionInhertTestClass which holds three testclasses that are run by the custom FxCop-rule.其中有一个主类ExceptionInhertTestClass ,它包含三个由自定义 FxCop 规则运行的测试类。

This custom rule checks the namespace of the class and compares it to the class it inherits from.此自定义规则检查类的命名空间并将其与继承自的类进行比较。

When debugging the rule, I noticed that the there was an unexpected problem on the MySecondException -class.在调试规则时,我注意到MySecondException类上存在意外问题。 There was no Namespace found for this class, at all.根本没有找到这个类的命名空间。 The picture demonstrates:图片展示了:

图片

Why does the class not have a value for its' namespace-property, while it obviously should have?为什么这个类没有它的命名空间属性的值,而它显然应该有?

Actually, it's arguable whether it should have a namespace or not.实际上,它是否应该具有命名空间是有争议的。 According to the .NET CLI spec , the metadata for a nested type should actually have a null namespace (see point 11 on p.223).根据.NET CLI 规范,嵌套类型的元数据实际上应该有一个空命名空间(参见第 223 页的第 11 点)。 That said, the FxCop API doesn't necessarily need to expose metadata in a way that corresponds to the spec.也就是说,FxCop API 不一定需要以符合规范的方式公开元数据。 However, since Microsoft doesn't provide an SDK for FxCop rule creation, there is no way to know what might have been intended here.但是,由于 Microsoft 不提供用于 FxCop 规则创建的 SDK,因此无法知道此处的意图。

In other words, now that you know that the namespace for a nested type isn't exposed in this way, you should climb its declaration hierarchy to find the appropriate namespace if you need it.换句话说,既然您知道嵌套类型的名称空间不会以这种方式公开,您应该爬上它的声明层次结构以找到合适的名称空间(如果需要)。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使其与 class 位于同一命名空间中的类可以访问变量,但命名空间之外的类不在 C# 中? - How do I make it so classes in the same namespace as a class have access to a variable but classes outside of a namespace don't in C#? ActiveX组件无法创建对象:在注册dll时没有名称空间的类会发生什么情况? - ActiveX component can't create object: what happens to classes that don't have a namespace when the dll is registered? 当我没有放置命名空间时,我的类有什么命名空间? - What namespace are my classes in when I don't put a namespace? 从无法访问它们的类更新表单元素 - Updating form elements from classes that don't have access to them 如何让T4根据特定命名空间中的类生成代码(递归) - How can I have T4 generate code based on classes in a specific namespace (Recursively) Xamarin的绑定库生成没有类信息的DLL - Binding library for Xamarin produce DLL that don't have information about classes 扩展方法应该只用于您无权访问其代码的类吗? - Should extension methods only be used on classes whose code you don't have access to? 在个人类上使用字典时,为什么不必覆盖GetHashCode? - Why don't I ever have to override GetHashCode when using Dictionaries on personal classes? CustomControl,名称空间中不存在对象 - CustomControl, objects don't exist in namespace c#找不到类型或名称空间的嵌套类 - c# nested classes the type or namespace could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM