English 中文(简体)
采用相同接口的不同内容
原标题:Passing different content that implements the same interface

我有多个班轮,如“条款”“新闻”“宣传”。

他们都有一个头衔,他们都有一个独特的身份证,他们都有一份摘要。

因此,我设立了一个称为<代码>的接口。 缩略语

public interface IContent {
    int Id { get; set; }
    String Title { get; set; }
    String Summary { get; set; }
    String HyperLink { get; set; }
}

在我的法典中,Im试图通过List<T>,实施IContent,然后使用我在项目中每个部分课堂上实施的共同财产。

因此,只是澄清问题

Article is a Linq Entity. I create a partial class and implement IContent Here s a snippet of Article.cs:

   #region IContent Members

    public int Id {
        get {
            return this.ArticleID;
        }
        set {
            this.ArticleID = value;
        }
    }

简单。 我的法典Im试图这样做,但我不知道我在哪里会错:

List<IContent> items;

MyDataContext cms = new MyDataContext();

items = cms.GetArticles();  
// ERROR: Can not implicitly convert List<Article> to List<IContent>

如果我的<条码>第类别实施<条码> 我不知道将要通过的目标是什么。

我知道,我可以这样做,选择一个基类,但使用Linq ToSQL并不使用固定物体。

我确信,这只是我空洞的。

最佳回答

items = cms.GetArticles().Cast<IContent>().ToList();  
问题回答

这是因为清单类别和接口都是var。 如果你重新使用4.0和C# 4.0,你可以实际使用<代码>上的同var。 IE amountable<> cross, which should be okay for LINQ application.

对这些专题有一个良好的财务结构。

这是在C# 4.0中要求共同实现的典型例子。 仅需要改动<代码>List<Article>至IE 数可计算和lt;Article>,以使这一指定工作:

IEnumerable<IContent> articles = myContext.GetArticles();

如果你 st。 NET 3.5 您只能使用LinqCast<T>():

IEnumerable<IContent> articles = myContext.GetArticles().Cast<IContent>();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签