2016-04-24 43 views
-1

我想如下創建使用VS2005在C#類:C#IList的參數會導致不一致的無障礙

public class MyClass 
{ 
     private string reference; 
     private IList<MyClassItem> items; 

     public MyClass(string reference, IList<MyClassItem> items) // error here 
     { 
      this.reference = reference ; 
      this.items = items; 
     } 

     public string Reference { get { return this.reference ; } set { this.reference = value; } } 
     public IList<MyClassItem> Items { get { return this.items; } set { this.items = value; } } // error line 
} 

public class MyClassItem 
{ 
     private string id; 
     private string name; 

     public MyClassItem(string id, string name) 
     { 
      this.id= id; 
      this.name= name; 
     } 
     public string Id{ get { return this.id; } set { this.id= value; } } 
     public string Name { get { return this.name; } set { this.name= value;}} 

} 

我得到了錯誤:

Inconsistent accessibility: parameter type ' System.Collections.Generic.IList<Library.Model.MyClassItem> ' is less accessible than method ' Library.Model.MyClass.Jurnal(string, System.Collections.Generic.IList<Library.Model.MyClassItem>) ' D:...\MyClass.cs 15 16 Library Error 4 Inconsistent accessibility: property type ' System.Collections.Generic.IList<Library.Model.MyClassItem> ' is less accessible than property 'Library.Model.MyClass.Items' D:...\MyClass.cs 28 34 Library

+5

絕對*您當然可以* MyClassItem是公開的嗎?你能否提供證明問題的[mcve]?這些類是否嵌套? –

+0

發佈的代碼不會導致任何錯誤。複製/粘貼或縮短代碼時發生錯誤。 –

+0

@JonSkeet對我錯過了我的代碼中MyClassItem中的'public'...謝謝你們有一個美好的一天 – Jackie

回答

0

唯一的問題我可以想像IST是您將MyClassItem定義爲其他類別的一部分,例如:私人的,受保護的或內部的,例如:

internal class MyOtherClass 
{ 
    //MyOtherClass properties and functions 

    public class MyClassItem 
    { 
    } 

} 

只需將MyClassItem直接移動到命名空間中,這應該可以解決問題