【问题标题】:Inconsistent accessibility: field type is less accessible than field [duplicate]可访问性不一致:字段类型的可访问性不如字段 [重复]
【发布时间】:2018-03-07 06:49:07
【问题描述】:

我终于开始在我们的旧 API 中实现 Linq,所以我开始为数据库创建与我们的表和 DB 类相关的所有类,我遵循了一个在线指南,但我无法让它工作。我的 Company.cs 有以下代码:

using RAW_API.Models;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace RAW_API.DataContexts {
    [Database]
    public class Company : DataContext {
        public Table<NewsItems> news_items;
        public Company( string connection ) : base( connection ) { }
    }
}

对于我的 NewsItems.cs 类文件:

using System;
using System.Data.Linq.Mapping;

namespace RAW_API.Models {
    [Table( Name = "news_items" )]
    public class NewsItems {
        [Column( IsPrimaryKey = true, IsDbGenerated = true )]
        public int id { get; set; }
        [Column]
        public string titleNL { get; set; }
        [Column]
        ...
    }
}

所有类和字段都是公开的,但仍然会抛出以下错误:

错误 CS0052:可访问性不一致:字段类型 “表”比字段更难访问 'Company.news_items' ApplicationServerWrapper K:\Group\repo_groclaes_rawapi\ApplicationServerWrapper\DataContexts\Company.cs:8

【问题讨论】:

  • Table 类型在哪里或是什么?
  • 您的Table 类型似乎不是public,而是您的班级NewsItems。正如@rene 所说,您的类型Table 是什么?它是如何定义的?使用什么访问修饰符?
  • @rene 这是来自 linq 的包含类,这让我感到困惑。 'class System.Data.Linq.Table where TEntity : class'

标签: c# linq datacontext


【解决方案1】:

可访问性不一致 错误意味着Table 类(即Table&lt;T&gt;)可以声明和初始化为private,将其设置为public,使其具有与@ 相同的accessibility level 987654327@.

因此,如果您在这样的地方有 Table 类:

// T is the table class name
class Table<T>
{
    // other stuff
}

您需要根据news_items字段的要求将其设置为public级别:

public class Table<T>
{
    // other stuff
} 

参考:

Inconsistent accessibility: field type 'world' is less accessible than field 'frmSplashScreen

【讨论】:

  • @Icepickle 我也这么认为,但如果是这种情况,错误应该显示TableAttribute
  • TableAttribute 在您的情况下与 Table&lt;T&gt; 不同...TableAttribute 不是通用的,必须与 [Table("TableName")] 一起使用,而不是声明为类型。
  • 如果我这样做,我会收到此错误:错误 CS1729 'TableAttribute' does not contain a constructor that requires 1 arguments NewsAPI K:\Group\repo_groclaes_rawapi\NewsAPI\Models\NewsItems.cs 5 Active跨度>
【解决方案2】:

来自 MS DOCs

编译器错误 CS0052
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0052

// CS0052.cs
public class MyClass2
{
   // The following line causes an error because the field, M, is declared
   // as public, but the type, MyClass, is private. Therefore the type is
   // less accessible than the field.
public MyClass M;   // CS0052

private class MyClass
{
}
   // One way to resolve the error is to change the accessibility of the type
   // to public. 
   //public class MyClass
   // Another solution is to change the accessibility of the field to private.
   //  private MyClass M; 
}

public class MainClass
{
    public static void Main()
    {
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2012-10-11
    • 2014-06-25
    • 2014-05-26
    相关资源
    最近更新 更多