Ada*_*gen 11 c# partial-classes
我使用部分类来分割2个文件之间的一些功能,但我收到一个错误.我究竟做错了什么?
A1.cs:
private partial class A
{
private string SomeProperty { get { return "SomeGeneratedString"; } }
}
Run Code Online (Sandbox Code Playgroud)
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'
}
}
Run Code Online (Sandbox Code Playgroud)
Ram*_*Ram 13
与@Andrey K的答案相同,但简单来说
使用每个文件的"属性"窗口将所有部分类的构建操作设置为"编译"
起初,我无法重现您的错误。
当这些局部类单独定义时,在名称空间内,private关键字会导致构建失败,并出现“无法将名称空间中定义的元素显式声明为private,protected或protected内部”。
如果我将它们设为私有并嵌套在另一个类中,则一切正常。
仅当在一个文件中,该类的一部分嵌套在另一个类中,而在另一个文件中,我不嵌套该类,然后删除private关键字时,才可以重现您的错误:
Class1.cs:
namespace stackoverflow.answers
{
public class Foo
{
private partial class Bar
{
private string SomeProperty { get { return "SomeGeneratedString"; } }
}
}
}
Run Code Online (Sandbox Code Playgroud)
Class2.cs:
namespace stackoverflow.answers
{
partial class Bar
{
void SomeFunction()
{
string bar = this.SomeProperty;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果名称空间不同,我也会得到您描述的错误。
请发布解决方案的完整代码,因为提供的代码是无效的C#语法,没有更多上下文就无法查看。
| 归档时间: |
|
| 查看次数: |
10180 次 |
| 最近记录: |