Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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#_Scope_Partial Classes - Fatal编程技术网

C# 将类文件划分为部分类会影响作用域吗?

C# 将类文件划分为部分类会影响作用域吗?,c#,scope,partial-classes,C#,Scope,Partial Classes,我有一个类库项目,我的命名空间/主类如下所示: 文件:Document.cs namespace HtmlEngine { public class Document : IDisposable { ... public class DocumentActionReplaceByTag : IDocumentAction { 所有这些都很好,在另一个项目/组装中,我提出:

我有一个类库项目,我的命名空间/主类如下所示:

文件:Document.cs

     namespace HtmlEngine 
     {

         public class Document : IDisposable
         { ...

               public class DocumentActionReplaceByTag : IDocumentAction
               {
所有这些都很好,在另一个项目/组装中,我提出:

using HtmlEngine;

...

DocumentActionReplaceByTag currentDocAction = new HtmlEngine.DocumentActionReplaceByTag("[NEXT_PART]");
而且它工作得很好。但是,我现在已经将该文档类文件分为几个文件,称为DocumentActions.cs、DocumentSections.cs,并将主要功能保留在Document.cs文件中。在每一条的顶部,我写下:

public partial class Document : IDisposable
{
现在,在消费项目中,我得到一个“无法解析符号”DocumentActionReplaceByTag错误。我仍然在使用对HtmlEngine的引用

在网上我能找到的最贴切的东西是这篇文章,它同样描述了我的困境,但他不太清楚发生这种情况的原因:

我一直认为分部类是语法糖,在编译之前它们被组合成一个类。我为每个分部类声明重复了接口,不确定这是否是一个因素


为什么这会超出范围

如果我正确理解了您的代码,那么创建新DocumentActionReplaceByTag的行必须如下所示:

DocumentActionReplaceByTag currentDocAction = new HtmlEngine.Document.DocumentActionReplaceByTag("[NEXT_PART]");
那么IDisposable呢,它只能在一个部分文件中指定

Glinkot,从提供的代码中,我看到您的DocumentActionReplaceByTag类被放入Document类中。下面的示例显示嵌套类始终可以通过ParentClass访问。请注意,我的所有类都在同一个文件中,代码不会编译

    namespace SomeNamespace
{
    public class ParentClass
    {
        public class NestedClass { }

                public void SomeMethod()
                {
                    // This compiles, since nested class is used inside parent class
                    NestedClass nestedClass = new NestedClass();
                }
    }

    public class AnotherClass
    {
        public AnotherClass()
        {
            // Not compiles since "NestedClass" is defined as nested class
            var nestedClass = new NestedClass();
            // Will compiles
            var nestedClass = new ParentClass.NestedClass();

            // Not compiles since "NestedClass" is defined as nested class
            NestedClass nestedClass = new NestedClass();
            // Will compiles
            ParentClass.NestedClass nestedClass = new ParentClass.NestedClass();
        }
    }
}

如果您有其他情况,请提供更多详细信息。

谢谢Andris。这是正确的,添加显式引用可以完成这项工作,但我希望继续访问DocumentActionReplaceByTag而不使用前缀。这就是在划分班级之前的工作方式。我确实从其他文件中删除了IDisposable,但这仍然没有什么区别。我不能使用HtmlEngin.Document添加,因为这不是一个名称空间-我的问题是为什么它的行为不同,希望我能做些什么来解决它。ThxI添加示例,使用嵌套类。如果你觉得我不了解你的情况,请提供更多细节。你好,安德里斯。在本例中,请参见原始问题中的desc,我使用的是来自不同程序集/命名空间的类。当类位于单个文件中时,嵌套类可以在没有限制的情况下实例化,但是当我将其划分为部分类时,这就不再有效了!根据我问题链接中的描述。我不明白为什么我需要用parentclass来证明每个人的资格。如果归结到这一点,我只需要重新组合类,并使用区域。也许这样更好,但我仍然想知道为什么范围会发生变化。