我有一个公开的类,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;
}
}
构造函数是公共的。我甚至试图更换所有&#34;私人&#34;用&#34; public,&#34;但它仍然说它无法进入。这是我尝试调用它的行:
private static IArticleDao _articleDao;
public static IArticleDao ArticleDao
{
get { return _articleDao ?? (_articleDao = new ArticleDao()); }
}
它所说的&#34; _articleDao = new ArticleDao()&#34;是错误的地方。
我特别困惑,因为创建了ArticleDao和IArticleDao,我基本上只是复制了EventDao和IEventDao并取代了#34; Event&#34;用&#34;文章。&#34;这有效:
private static IEventDao _eventDao;
public static IEventDao EventDao
{
get { return _eventDao ?? (_eventDao = new EventDao()); }
}
但ArticleDao不起作用。
答案 0 :(得分:3)
班级本身不公开。它是internal
。 (任何事物的默认可访问性是最小的合法可访问性选项,对于非嵌套类,它是internal
。您没有明确指定任何可访问性选项。)
答案 1 :(得分:2)
您class ArticleDao
是内部,就像您没有指定任何辅助功能修饰符一样,C#
中的默认设置是内部的。要解决此问题,您可以考虑将其声明为 public
public class ArticleDao : IArticleDao
{
...
}
答案 2 :(得分:0)
班级&#39;默认访问级别是内部的。内部类型或成员只能在同一程序集中的文件中访问。
您可能想要指定
公共类ArticleDao ......
构造函数的可访问性与类访问级别不同,如果类访问修饰符隐藏了某些内容,则无论访问修饰符如何,都无法访问其任何成员。