以下 c# 代码出现不一致的可访问性错误。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/524761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inconsistent accessibility error with the following c# code. Why?
提问by Crippeoblade
Whats wrong with the following c# code? Compiler reports this error:
以下 c# 代码有什么问题?编译器报这个错误:
Inconsistent accessibility: parameter type 'ClassLibrary1.Interface1' is less accessible than method 'ClassLibrary1.Class1.Class1(ClassLibrary1.Interface1)'
不一致的可访问性:参数类型“ClassLibrary1.Interface1”的可访问性低于方法“ClassLibrary1.Class1.Class1(ClassLibrary1.Interface1)”
with the following code:
使用以下代码:
interface Interface1<T>
{
bool IsDataValid();
/* Other interfaces */
}
public class Class1<T>
{
public Interface1<T> interface1;
public Class1(Interface1<T> interface1)
{
this.interface1 = interface1;
}
}
I've since designed my code differently using inheritence to but if anyone could tell me what the above is wrong I'd greatly appreciate it.
从那以后,我使用继承以不同的方式设计了我的代码,但如果有人能告诉我上述错误是什么,我将不胜感激。
采纳答案by Sciolist
your "Interface1" isn't public..
你的“Interface1”不是公开的..
public interface Interface1<T>
{
bool IsDataValid();
/* Other interfaces */
}
回答by Eric King
Mark your interface as public:
将您的界面标记为公开:
public interface Interface1<T>
public interface Interface1<T>
If you leave out the accessibility label, it defaults to internal
, that is, only accessible to other classes within the assembly.
如果您省略可访问性标签,则它默认为internal
,即只有程序集中的其他类才能访问。
回答by vijay
second solution is If your interface is not public then dont make your class public where you are making a handle of interface.
第二种解决方案是如果您的接口不是公开的,那么不要在您制作接口句柄的地方公开您的类。