C# 部分类的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/184970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:02:24  来源:igfitidea点击:

Error with C# Partial classes

c#partial-classes

提问by Adam Tegen

I am using partial classes to split some functionality between 2 files, but I am getting an error. What am I doing wrong?

我正在使用部分类在 2 个文件之间拆分某些功能,但出现错误。我究竟做错了什么?

A1.cs:

A1.cs:

private partial class A
{
    private string SomeProperty { get { return "SomeGeneratedString"; } }       
}

A2.cs:

A2.cs:

private partial class A
{
    void SomeFunction()
    {
        //trying to access this.SomeProperty produces the following compiler error, at least with C# 2.0
        //error CS0117: 'A' does not contain a definition for 'SomeProperty'
    }
}

采纳答案by Sklivvz

Are the two partial classes in the same namespace? That could be an explanation.

两个部分类是否在同一个命名空间中?这可能是一种解释。

回答by Matt Brunell

different namespace?

不同的命名空间?

回答by Matt Hamilton

I think it's because you're declaring your class as "private". Try changing the modifier to "internal" so that the two "halves" of the class can "see" each other within the same assembly.

我认为这是因为您将课程声明为“私有”。尝试将修饰符更改为“内部”,以便类的两个“半”可以在同一个程序集中“看到”彼此。

回答by John Kraft

The error I get is:

我得到的错误是:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
命名空间中定义的元素不能显式声明为私有、受保护或受保护的内部

I'm guessing it's a namespace issue as previously stated.

我猜这是如前所述的命名空间问题。

回答by Troy Howard

At first, I was unable to reproduce your error.

起初,我无法重现您的错误。

When these partial classes are defined alone, inside a namespace, the private keyword causes the build to fail with "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"...

当在命名空间内单独定义这些部分类时,private 关键字会导致构建失败,并显示“命名空间中定义的元素不能显式声明为私有、受保护或受保护的内部”...

If I keep them private and nest them within another class, everything works fine.

如果我将它们保密并将它们嵌套在另一个类中,则一切正常。

I can reproduce your error only when, in one file, I have part of the class nested inside another class, and in another file, I do NOT nest the class, and then remove the private keyword... like this:

只有当在一个文件中,我将类的一部分嵌套在另一个类中,而在另一个文件中,我不嵌套类,然后删除 private 关键字时,我才能重现您的错误......像这样:

Class1.cs:

Class1.cs:

namespace stackoverflow.answers
{
    public class Foo
    {
        private partial class Bar
        {
            private string SomeProperty { get { return "SomeGeneratedString"; } }
        }
    }
}

Class2.cs:

Class2.cs:

namespace stackoverflow.answers
{
    partial class Bar
    {
        void SomeFunction()
        {
            string bar = this.SomeProperty;
        }
    }    
}

I also get the error you described if the namespaces differ.

如果命名空间不同,我也会收到您描述的错误。

Please post the entire code for the solution, because the provided code is invalid C# syntax, and can't be looked into without more context.

请发布解决方案的完整代码,因为提供的代码是无效的 C# 语法,如果没有更多上下文就无法查看。

回答by sada

I analyze your code. you declared partial class as nested class this is cause to show error. why bcz partial class will not declare as nested class the parial keyword is split into ultiple files so, every file nae is same when you declared in nested class it will not recognize.

我分析你的代码。您将部分类声明为嵌套类,这会导致显示错误。为什么 bcz 部分类不会声明为嵌套类 parial 关键字被拆分为多个文件,因此,当您在嵌套类中声明时,每个文件 nae 都是相同的,它无法识别。

回答by Andrey K.

Edit:

编辑:

solution: build action -> Complile, nothing else

解决方案:构建动作 -> 编译,没有别的

I'll try to be more specific:

我会尝试更具体:

I had a class shared among 3 partial classes in 3 different files. At one moment a function call (from one part, of a function declared in other part) started showing error "does not exist in current context". It took me long until some guy helpedme to figure out that accidentally i set the build action of the file with one part to "EmbeddedResourse" instead of "Compile".

我在 3 个不同文件中的 3 个部分类之间共享了一个类。在某一时刻,一个函数调用(从一个部分开始,在另一部分中声明的函数)开始显示错误“当前上下文中不存在”。我花了很长时间,直到有人我弄清楚我不小心将文件的构建操作设置为“EmbeddedResourse”而不是“Compile”。

To switch build action you should right click on file in solution explorer, then choose properties. Then change the build action.

要切换构建操作,您应该右键单击解决方案资源管理器中的文件,然后选择属性。然后更改构建操作。

This question is old, and my answer is not exactly helpful in this very case. But it may be helpful in one other very rare case related to partial classes. Sorry if my English is not clear.

这个问题很老了,我的回答在这种情况下并不完全有帮助。但在另一种与部分类相关的非常罕见的情况下,它可能会有所帮助。对不起,如果我的英语不清楚。

回答by Ram

Same answer as @Andrey K but in simple terms

与@Andrey K 相同的答案,但简单来说

Set the build action of all your partial classes to 'Compile' using the 'Properties' windows of each of those files

使用每个文件的“属性”窗口将所有部分类的构建操作设置为“编译”

Properties window ->Build action property

属性窗口 ->构建动作属性

回答by Vic Seedoubleyew

A corner case where additional help can save you time is if you are using a partial class to complement a Run-time Text Template's generated class.

如果您使用分部类来补充运行时文本模板的生成类,则额外帮助可以节省您时间的一个极端情况是。

Chances are that the problem is indeed that your partial class' namespace is different from the namespace of the generated part of that class. To check that, simply look at the generated code.

有可能问题确实在于您的分部类的命名空间与该类的生成部分的命名空间不同。要检查这一点,只需查看生成的代码。

To fix that, you need to edit the .csprojfile for your project, and in the section about that template, add the <ClassNamespace>tag:

要解决此问题,您需要编辑.csproj项目的文件,并在有关该模板的部分中添加<ClassNamespace>标签:

<Content Include="MyTemplate.tt">
  <Generator>TextTemplatingFilePreprocessor</Generator>
  <ClassNamespace>My.Namespace</ClassNamespace>
  <LastGenOutput>MyTemplate.cs</LastGenOutput>
</Content>

回答by Anton Andreev

All the files should be in the same folder.

所有文件都应该在同一个文件夹中。