【问题标题】:Public class is inaccessible due to protection level; Constructor is public由于保护级别,公共类不可访问;构造函数是公开的
【发布时间】:2015-02-25 18:32:16
【问题描述】:

我有一个公共类 ArticleDao,但是当我尝试在另一个文件中使用它时,它显示“ArticleDao 由于其保护级别而无法访问”。这是整个班级:

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”替换所有“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 不起作用。

【问题讨论】:

  • 这就是为什么我会劝阻您不要依赖默认的可访问性选项,作为一般规则。而不是尝试记住所有关于默认值、时间、语言的详细规则,并假设所有其他编写代码的程序员也将始终记住它,并且还假设您也知道并希望默认行为,更容易总是明确地说明可访问性。做起来很简单,也很重要。

标签: c# class private public protection


【解决方案1】:

课程本身不是公开的。这是internal。 (任何东西的默认可访问性是最小的合法可访问性选项,对于非嵌套类,它是internal。您没有明确指定可访问性选项。)

【讨论】:

  • 那我该如何解决呢?它与 EventDao.cs 在同一个项目文件夹中,我可以访问 EventDao.cs
  • @user3784238 如果您希望该课程是公开的,请将其公开。
【解决方案2】:

class ArticleDao是一个internal,好像你没有指定任何可访问性修饰符,C#中的默认是internal。要解决此问题,您可能会考虑将其声明为 public

public class ArticleDao : IArticleDao
{
   ...
}

【讨论】:

  • 它不是私人的,它是internal。 (除非它是嵌套的并且没有显示嵌套。)
  • 啊,我知道我错过了一些简单的东西。没注意到EventDao说的是“public class”,而ArticleDao只说的是“class”
【解决方案3】:

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

你可能想指定

公开课 ArticleDao...

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

【讨论】:

  • 非嵌套类的默认可访问性(在此处出现)是internal。如果该类是嵌套类,则默认可访问性为private
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 2012-12-13
  • 2011-05-30
  • 2019-08-24
  • 1970-01-01
相关资源
最近更新 更多