Public class 由于保护级别无法访问;构造函数是 public

Public class is inaccessible due to protection level; Constructor is public

我有一个 class,即 public,ArticleDao,但是当我尝试在另一个文件中使用它时,它说 "ArticleDao is inaccessible due to its protection level." 这是整个 class :

class ArticleDao : IArticleDao
{
    private readonly ContentManager _contentManager;

    private static readonly char[] DelimiterChars = { '|', '\n' };

    private const int ArticlePagingSize = 500;

    public ArticleDao()
    {
        _contentManager = new ContentManager();
    }

    private Image GetImage(XElement element)
    {
        var image = new Image();

        if (String.IsNullOrEmpty((String)element))
        {
            return image;
        }

        XElement imageElement = element.Element("img");
        if (imageElement != null)
        {
            image.Url = (String)imageElement.Attribute("src");
        }
        return image;
    }

    private Link GetLink(XElement element)
    {
        var link = new Link();

        if (String.IsNullOrEmpty((String)element))
        {
            return link;
        }

        XElement anchorElement = element.Element("a");
        if (anchorElement != null)
        {
            link.Url = (String)anchorElement.Attribute("href");
            link.Text = (String)anchorElement;
        }
        return link;
    }

    public Article GetArticle(long id, string html)
    {
        var a = new Article();
        long testid = 556;
        if (id == testid)
        {
            var x = 1;
        }

        XDocument xdoc = XDocument.Parse(html);
        var xElement = xdoc.Element("root");
        if (xElement != null)
        {
            XElement articleElem = xElement.Element("Event");
            if (articleElem != null)
            {
                a = new Article();
                a.Id = id.ToString(CultureInfo.InvariantCulture);
                a.Title = (String)articleElem.Element("Title");
                a.PublishedDate = GetDateTime((String)articleElem.Element("PublishedDate"));
                a.SubHeader = (String)articleElem.Element("SubHeader");
                a.Image = GetImage(articleElem.Element("Image"));
                a.Caption = (String)articleElem.Element("Caption");
                a.Body = (String)articleElem.Element("Body");
                a.Url = GetLink(articleElem.Element("Url"));

            }
        }

        return a;

    }

    public Article GetArticle(Int64 ektronContentId)
    {
        var item = _contentManager.GetItem(ektronContentId);
        return GetArticle(ektronContentId, item.Html);
    }

    public IEnumerable<Article> GetArticles(Int64 folderId)
    {
        int count;
        IEnumerable<Article> articles = new List<Article>(ArticlePagingSize);
        do
        {
            var criteria = new ContentCriteria();
            criteria.AddFilter(ContentProperty.FolderId, CriteriaFilterOperator.EqualTo, folderId);
            criteria.PagingInfo.RecordsPerPage = ArticlePagingSize;

            var articleContentData = _contentManager.GetList(criteria);
            count = articleContentData == null ? 0 : articleContentData.Count;

            articles = articles.Concat(_contentManager.GetList(criteria)
                .Select(i => GetArticle(i.Id, i.Html)));
        } while (count == ArticlePagingSize);
        return articles;
    }

    private DateTime GetDateTime(string date)
    {
        DateTime dt;

        DateTime.TryParse(date, out dt);

        return dt;
    }
}

构造函数是public。我什至尝试用 "public," 替换 "private" 的所有实例,但它仍然说它无法访问。这是我试图调用它的行:

private static IArticleDao _articleDao;
public static IArticleDao ArticleDao
{
    get { return _articleDao ?? (_articleDao = new ArticleDao()); }
}

“_articleDao = new ArticleDao()”就是错误所在。

我特别困惑,因为要创建 ArticleDao 和 IArticleDao,我基本上只是复制了 EventDao 和 IEventDao 并将 "Event" 替换为 "Article." 这行得通:

private static IEventDao _eventDao;
public static IEventDao EventDao
{
    get { return _eventDao ?? (_eventDao = new EventDao()); }
}

但是 ArticleDao 不工作。

class 本身不是 public。是 internal。 (任何事物的默认可访问性是最小的合法可访问性选项,对于非嵌套 class,是 internal。您没有明确指定可访问性选项。)

class ArticleDaointernal,就好像您没有指定任何可访问性修饰符一样,C# 中的默认值是 internal。要解决此问题,您可能会考虑声明它 public

public class ArticleDao : IArticleDao
{
   ...
}

class' 默认访问级别是内部。内部类型或成员只能在同一程序集中的文件中访问。

您可能想要指定

public class ArticleDao...

构造函数可访问性与 class 访问级别不同,如果某些内容被 class 访问修饰符隐藏,则无论其访问修饰符如何,您都无法访问其任何成员。