What is inconsistent accessibility error?

一世执手 提交于 2019-12-11 10:17:34

问题


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();
    }
  }

All the methods are public.but it showing error that

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

Where I am doing wrong ?


回答1:


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

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


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?


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.

Make it public like this,

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();
    }
}



回答2:


You should make your RoleProvider class public also.

public class RoleProvider 
{
   ...
}

Base classes can't be less accessible than derived classes. Your RoleProvider class has probably has less access modifiers than CustomRoleProvider class.

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

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

    interface RoleProvider
    {
           void CreateRole(string roleName);

           bool DeleteRole(string roleName, bool throwOnPopulatedRole);
    }

Actually, you don't have to write public keyword on interfaces. Interface members are automatically public. They can't include any access modifiers.




回答3:


It means that you've defined RoleProvider ... but to be a private class (is this true?). Try making RoleProvider a public.




回答4:


Try changing access specifier of RoleProvider class to either public or internal.



来源:https://stackoverflow.com/questions/14625023/what-is-inconsistent-accessibility-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!