什么是可访问性不一致的错误? [英] What is inconsistent accessibility error?
问题描述
我使用自定义角色提供者,我做了一个 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屋!