如何解决错误:可访问性不一致:通用 c# 接口的参数类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16420669/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:58:45  来源:igfitidea点击:

How to solve the Error: Inconsistent accessibility: parameter type for generic c# interface?

c#

提问by VJain

On writting this code into my project i am getting the error that

将此代码写入我的项目时,我收到错误消息

Error 1 Inconsistent accessibility: field type 'System.Collections.Generic.List<Jain_milan.Childrendata>'is less accessible than field 'Jain_milan.addchild.m_children'
Error 2 Inconsistent accessibility: parameter type 'System.Collections.Generic.List<Jain_milan.Childrendata>'is less accessible than method 'Jain_milan.addchild.addchild(System.Collections.Generic.List<Jain_milan.Childrendata>)'

错误 1 ​​可访问性不一致:字段类型的'System.Collections.Generic.List<Jain_milan.Childrendata>'可访问性不如字段'Jain_milan.addchild.m_children'
错误 2 可访问性不一致:参数类型的'System.Collections.Generic.List<Jain_milan.Childrendata>'可访问性不如方法'Jain_milan.addchild.addchild(System.Collections.Generic.List<Jain_milan.Childrendata>)'

namespace Jain_milan
{
        public partial class addchild : Form
        {
            List<Label> label = new List<Label>();
            List<TextBox> textbox = new List<TextBox>();
            List<ComboBox> combobox = new List<ComboBox>();
            List<DateTimePicker> datetimepicker = new List<DateTimePicker>();
            public List<Childrendata> m_children = new List<Childrendata>();
            public addchild(List<Childrendata> children)
            {
                InitializeComponent();
                this.m_children = children; //Initialize the same List as sent by Mainform
            }

采纳答案by Luis Filipe

Without posting your entire relevant code i'll try a hunch:

在不发布您的整个相关代码的情况下,我会尝试预感:

the class Childrendatais declared as not-public and (as we can see) the variable m_childrenis public

Childrendata类被声明为非公开的,并且(如我们所见)变量m_children是公开的

Threfore a public variable cannot expose a less accessible type, in this case, Childrendata

因此,公共变量不能公开可访问性较低的类型,在本例中为Childrendata

Additionally, what you might want is to turn m_childrenprivate as well as this is usually the best practice

此外,您可能想要的是将m_children 设为私有,这通常是最佳实践

回答by Damien_The_Unbeliever

Childrendataisn't public. How, then, do you expect someone calling addchildto be able to provide the required parameter?

Childrendata不是公开的。那么,您如何期望呼叫者addchild能够提供所需的参数?

The obvious fixes are to change the accessibility of addchildor Childrendata.

显而易见的解决方法是更改addchildor的可访问性Childrendata

回答by D Stanley

My guess is that the Childrendataclass is private(or internal, or implicitly internalby not specifying a visibility modifier)

我的猜测是Childrendata该类是private(或internal,或internal通过不指定可见性修饰符隐式)

Since List<Childrendata> m_childrenis public, Childrendataneeds to be public as well.

既然List<Childrendata> m_children是公开的,也Childrendata需要公开。

Change Childrendatato publicand you should be fine.

更改Childrendatapublic,你应该罚款。

回答by Al-Hanash Moataz

just make the method addChilde(List<object> childeren)or only object not list then do this

只需制作方法addChilde(List<object> childeren)或仅对象未列出然后执行此操作

var Listchild = childeren as List<childe>;

回答by Naveed Ali

You can't use access modifier two times. if you used public class than you should not use the instance public. public partial class Record : ContentPage { List datas = new List() { I used record class public but not list.

您不能两次使用访问修饰符。如果您使用公共类,则不应使用公共实例。public partial class Record : ContentPage { List datas = new List() { 我使用了公共记录类,但没有使用列表。