如何通过"字典< X,类>"因为字典< X,接口与GT;"? [英] How to pass "Dictionary<x,Class>" as Dictionary<x,Interface>"?

查看:164
本文介绍了如何通过"字典< X,类>"因为字典< X,接口与GT;"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新建C#和#1 ......请随时指出这应该是不同的或其他地方问

New to both C# and Stackoverflow...please feel free to indicate if this should be asked differently or elsewhere.

假设:

public interface IPoint {...}
public struct DeliveryPoint : IPoint {...}

和生成一个字典,大量的巡回点代码:

and code that generated a dictionary with a large number of delivery points:

...
Dictionary<uint,DeliveryPoint> dict = new Dictionary<uint,DeliveryPoint>();
...



中的点现在需要被传递到需要该接口例行类型:

The points now need to be passed to a routine which requires the interface type:

public void doSomething( Dictionary<uint,IPoint> dict ) { ...}

想我能够做到这样的:

doSomething( dict );

只有解决办法,我发现是创建一个新的列表和复制所有的点,这似乎击败已实施摆在首位接口的整个目的。

Only solution I've found is to create a new List and copy all of the points which seems to defeat the whole purpose of having implemented the interface in the first place.

有没有更好的方法吗?

推荐答案

请通用在那里的类型实现接口的值的方法。

Make the method generic over the values where the type implements the interface.

public void DoSomething<TPoint>(IDictionary<uint, TPoint> dict) where TPoint : IPoint
{
    // do stuff
}

这篇关于如何通过&QUOT;字典&LT; X,类&GT;&QUOT;因为字典&LT; X,接口与GT;&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆