[英]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.