什么是可访问性不一致的错误? [英] What is inconsistent accessibility error?

查看:184
本文介绍了什么是可访问性不一致的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义角色提供者,我做了一个 CustomRoleProvider 类,并在它实施了一些 RoleProvider 方法,像这样

i am using custom role provider for that i made a CustomRoleProvider class and implemented some RoleProvider methods in it, like this

 public class CustomRoleProvider: RoleProvider
 {
 public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }
  }

所有的方法都是public.but它显示错误

All the methods are public.but it showing error that

错误4可访问性不一致:基类
  RoleProviderExample.RoleProvider比类不易进入
  RoleProviderExample.CustomRoleProvider。

Error 4-Inconsistent accessibility: base class 'RoleProviderExample.RoleProvider' is less accessible than class 'RoleProviderExample.CustomRoleProvider'.

我哪里做错了?

推荐答案

的基类 RoleProvider ,你是通过暴露 CustomeRoleProvider 不是公共

The base class RoleProvider that you are exposing via CustomeRoleProvider is not public.

如果您声明 RoleProvider 公共错误会自行消失。你不必给 RoleProvider A 公共构造。

If you declare RoleProvider as public the error will go away. You don't have to give RoleProvider a public constructor.

另外,您可以 CustomRoleProvider 的无障碍环境降低到 RoleProvider 的。这可能是最合适的答案,你需要公开 CustomRoleProvider 组装外?

Alternatively you could reduce the accesibility of CustomRoleProvider to that of RoleProvider. This may be the most appropriate answer, do you need to expose CustomRoleProvider outside the assembly?

如果 RoleProvider 是一个接口,那么,按照惯例,这是名不副实的,您可以将其重命名为 IRoleProvider 。在任何情况下,它仍然是少入店那么 CustomRoleProvider

If RoleProvider is an interface then, by convention, it is misnamed, you could rename it to IRoleProvider. In any case, it is still less accesible then CustomRoleProvider.

设为公开这个样子,

public interface IRoleProvider
{
    void CreateRole(string roleName);

    bool DeleteRole(string roleName, bool throwOnPopulatedRole);
}

如果您不指定接口的无障碍环境,结构内部是不言而喻的。

If you don't specify the accesibility of an interface, class or struct, internal is implied.

接口成员总是公共结构成员私人除非另有说明。

interface members are always public. class and struct members are private unless specified.

这是指定好的做法,只是在接口成员,其中总是公共

It is good practice to specify, except in the case of interface members which are always public.

您实现可能是这样的,在覆盖关键字不应该被用于接口实现,除非它们被覆盖可重写的基类实现。

Your implementation could look like this, the override keyword should not be used for interface implementations, unless they are overriding an overridable base class implementation.

public class CustomRoleProvider : IRoleProvider
{
    public void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }
}

如果你想明确地实现接口,

if you want to explicity implement the interface,

public class CustomRoleProvider : IRoleProvider
{
    public void IRoleProvider.CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public bool IRoleProvider.DeleteRole(
        string roleName, 
        bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }
}

这篇关于什么是可访问性不一致的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆