English 中文(简体)
能够缩短接口
原标题:can t shorten the interface
  • 时间:2010-10-04 21:43:25
  •  标签:
  • c#
  • .net
  • linq

我有这种财产,正在做罚款:

public IEnumerable<IGrouping<MessageType, Message>> MessageGroups
{
    get
    {
        return
            (from msg in _messages
                orderby msg.Type descending
                group msg by msg.Type);
    }
}

然而,这促使我不得不在我的法典中的若干地方重复“看望”的代码:可计数的编码;IGrouping<MessageType, Message;>。 我试图通过界定三维包装接口,使眼中更容易做到:

public interface IMessageGroups :
    IEnumerable<IGrouping<MessageType, Message>> { }

将上述财产改为:

public IMessageGroups MessageGroups
{
    get
    {
        return
            (IMessageGroups)
            (from msg in _messages
                orderby msg.Type descending
                group msg by msg.Type);
    }
}

这提高了罚款,但从现在起,我得到:

Unable to cast object of type System.Linq.GroupedEnumerable`3[Message,MessageType,Message] to type IMessageGroups .

(从错误信息中删除的针对具体项目的名称空间)

我可以做些什么来解决这个问题?

最佳回答

http://msdn.microsoft.com/en-us/library/sf0df423.aspx”rel=“nofollow”

using Foo = IEnumerable<IGrouping<MessageType, Message>>;

之后:

public Foo MessageGroups
{
    get
    {
        return
            (from msg in _messages
             orderby msg.Type descending
             group msg by msg.Type);
    }
}

另一种可能性是进一步扩大贵国的LINQ查询和elect。 某些习俗类型:

public IEnumerable<Foo> MessageGroups
{
    get
    {
        return
            (from msg in _messages
             orderby msg.Type descending
             group msg by msg.Type
             select new Foo { Messages = g, MessageType = g.Key }      
            );
    }
}

and:

public class Foo
{
    public MessageType MessageType { get; set; }
    public IEnumerable<Message> Messages { get; set; }
}

如果你不关心 la评价,你可以采用<代码>。

问题回答

由于汇编者永远无法知道所交回的类型是否实际上是一个子级,可以实施接口,因此,它会打造罚款。 (从那时起,用一对一对一类的投放,而从那时起,你就出现了一种汇编错误。

但是,事实仍然是,你试图将准则的表述结果转化为它没有执行的接口(“wrapper”接口)。 这只是要工作。 而你却无法在宣布某类人执行该分类的情况下确定该等级,实际上在你执行过程中“鞭pping”(也许通过现有的准则准则向施工者表达)。

<代码>IMessageGroups的投稿由于质询的结果是。 IEvidable<IGrouping<MessageType, 致辞>>, not an instance of IMessagegroups。 但是,你可以撰写<代码>MessageGroups的类别,并用该编码总结提问的结果:

public class MessageGroups : IMessageGroups
{
    private readonly IEnumerable<IGrouping<MessageType, Message>> _groups;

    public MessageGroups(IEnumerable<IGrouping<MessageType, Message>> groups)
    {
        _groups = groups;
    }

    public IEnumerable<IGrouping<MessageType, Message>> GetEnumerator()
    {
        return _groups.GetEnumerator();
    }

    public static MessageGroups Create(IEnumerable<IGrouping<MessageType, Message>> groups)
    {
        return new MessageGroups(groups);
    }
}

并使用:

public IMessageGroups MessageGroups
{
    get
    {
        return
            MessageGroups.Create(
                from msg in _messages
                orderby msg.Type descending
                group msg by msg.Type);
    }
}

在您的档案中:

 using ASimpleName = Dictionary<string, Dictionary<string, List<string>>>;

或者在您的情况下:

using ShortName = IEnumerable<IGrouping<MessageType, Message>>;




相关问题
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. ...

热门标签