简体   繁体   English

由于其保护级别,类无法访问

[英]Class is inaccessible due to its protection level

I have three classes.我有三个班级。 all are part of the same namespace.都是同一个命名空间的一部分。 here are the basics of the three classes.这是三个类的基础知识。

//FBlock.cs
namespace StubGenerator.PropGenerator
{
    class FBlock : IDesignRegionInserts, IFormRegionInserts, IAPIRegionInserts,  IConfigurationInserts, ISoapProxyClientInserts, ISoapProxyServiceInserts
    {
        private List<Property> pProperties;
        private List<Method> pMethods;
        public FBlock(string aFBlockName)
        { 
            pProperties = new List<Property>();
            pMethods = new List<Method>();
        }

        public Property AddProperty(string aName)
        {
            Property loProp = new Property(this, aName, pProperties.Count);
            pProperties.Add(loProp);
            return loProp;
         }

         public Method AddMethod(string aName)
         {
             Method loMeth = new Method(this, aName);
             pMethods.Add(loMeth);
             return loMeth;
         }
     }

 //Method.cs
 namespace StubGenerator.PropGenerator
 {
     class Method : IPropertyName
     {
         private List<StubGenerator.PropGenerator.PropertyAttribute> pPropertyAttributes;
         private string pName;
         private string pFBlockName;

         public Method(FBlock aFBlock,string aName)
         {
             pPropertyAttributes = new List<PropertyAttribute>();
             pName = aName;
             pFBlockName = aFBlock.Name;
         }
      }
 }

 //Property.cs
 namespace StubGenerator.PropGenerator
 {
    class Property : StubGenerator.PropGenerator.IPropertyName, StubGenerator.PropGenerator.IDesignRegionInserts, StubGenerator.PropGenerator.IFormRegionInserts, IAPIRegionInserts, IConfigurationInserts, ISoapProxyClientInserts, ISoapProxyServiceInserts
    {
        private string pName;
        private string pExpandedName;
        private string pFBlockInitials;

        private Group pPropertyGroup;
        private FlowLayoutPanel pGroupFlowPanel;
        private Button pUpdateButton;
        private CheckBox pShowProperty;


         private string pFBlockName;


         public Property(FBlock aFBlock, string aName, int aIndex)
         {
             pPropertyAttributes = new List<PropertyAttribute>();
             pFBlockName = aFBlock.FBlockName;

             ExpandName();
             GetInitials();

             pShowProperty = new CheckBox(this, 10, (aIndex + 1) * 20, aIndex);
             pPropertyGroup = new Group(this);
             pGroupFlowPanel = new FlowLayoutPanel(this);

             pUpdateButton = new Button(this, 10, 18, aIndex);
         }
     }
}

I'm getting the following errors我收到以下错误

'StubGenerator.PropGenerator.Method' is inaccessible due to its protection level 'StubGenerator.PropGenerator.Method' 由于其保护级别而无法访问

which refers to the following line in the FBlock.cs file它指的是 FBlock.cs 文件中的以下行

private List<Method> pMethods;

and

'StubGenerator.PropGenerator.Method' is inaccessible due to its protection level 'StubGenerator.PropGenerator.Method' 由于其保护级别而无法访问

which refers to the following line in the FBlock.cs file它指的是 FBlock.cs 文件中的以下行

 public Method AddMethod(string aName)

and

Inconsistent accessibility: return type 'StubGenerator.PropGenerator.Method' is less accessible than method 'StubGenerator.PropGenerator.FBlock.AddMethod(string)'不一致的可访问性:返回类型 'StubGenerator.PropGenerator.Method' 比方法 'StubGenerator.PropGenerator.FBlock.AddMethod(string)' 更难访问

which refers to the following line in the FBlock.cs file它指的是 FBlock.cs 文件中的以下行

 public Method AddMethod(string aName)

making the class Method public does not resolve the errors.公开类 Method 并不能解决错误。 I can't figure out why I don't get the errors when calling the Property class.我不知道为什么在调用 Property 类时没有收到错误消息。 And I don't understand why making the Method class public doesn't fix the problem.而且我不明白为什么公开 Method 类并不能解决问题。

Any ideas?有什么想法吗?

Edited to ask.编辑问。 could there be some setting on the file that causes this?文件上是否有一些设置会导致这种情况?

First thing, try a full rebuild.首先,尝试完全重建。 Clean and build (or just use rebuild).清理并构建(或仅使用重建)。 Every once in a long while that resolves bizarre build issues for me.每隔一段时间就会为我解决奇怪的构建问题。

Next, comment out the rest of the code that is not in your example you have posted.接下来,注释掉您发布的示例中没有的其余代码。 Compile.编译。 Does that work?那行得通吗?

If so, start adding segments back until one breaks it.如果是这样,请开始添加段,直到将其断开。

If not, make all the classes public and try again.如果没有,请将所有课程设为public然后重试。

If that still fails, maybe try putting the trimmed down classes in the same file and rebuilding.如果仍然失败,可以尝试将修剪过的类放在同一个文件中并重新构建。 At that point, there would be absolutely no reason for access issues.那时,绝对没有理由出现访问问题。 If that still fails, take up carpentry.如果仍然失败,请从事木工。

There was a project that used linked files.有一个使用链接文件的项目。 I needed to add the method.cs file to that project as a linked file as well, since the FBlock.cs file was there.我还需要将 method.cs 文件作为链接文件添加到该项目中,因为 FBlock.cs 文件在那里。 I've never heard of linked files before, I didn't even know that was possible.我以前从未听说过链接文件,我什至不知道这是可能的。

Try adding the below code to the class that you want to use尝试将以下代码添加到您要使用的类中

[Serializable()]
public partial class Class
{

也可能是包含相关类的库没有使用强名称正确签名。

The code you posted does not produce the error messages you quoted.您发布的代码不会产生您引用的错误消息。 You should provide a (small) example that actually exhibits the problem.您应该提供一个实际展示问题的(小)示例。

All your classes are internal by default默认情况下,您的所有课程都是internal课程

Marking public did not do the trick.标记为public并没有起到作用。

Are you sure you do not have two classes named Method, and perhaps are including the wrong Method class?您确定没有两个名为 Method 的类,并且可能包含错误的 Method 类吗?

I'm guessing public Method AddMethod(string aName) is defined on a public interface that FBlock implements.我猜public Method AddMethod(string aName)是在 FBlock 实现的公共接口上定义的。 Consumers of that interface are not guaranteed to have access to Method.不保证该接口的使用者可以访问 Method。

Hi You need to change the Button properties from private to public.嗨,您需要将 Button 属性从私有更改为公共。 You can change Under Button >> properties >> Design >> Modifiers >> "public" Once change the protection error will gone.您可以在按钮下>>属性>>设计>>修饰符>>“公共”更改保护错误将消失。

Budi布迪

your class should be public你的课程应该是公开的

public class FBlock : IDesignRegionInserts, IFormRegionInserts, IAPIRegionInserts, IConfigurationInserts, ISoapProxyClientInserts, ISoapProxyServiceInserts公共类 FBlock : IDesignRegionInserts、IFormRegionInserts、IAPIRegionInserts、IConfigurationInserts、ISoapProxyClientInserts、ISoapProxyServiceInserts

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM