ObservableCollection作为参数和接口传递 [英] ObservableCollection passed as parameter and Interfaces

查看:122
本文介绍了ObservableCollection作为参数和接口传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口IPerson和两个实现该接口的类AdventurerWorker.目前,我为冒险家和工作者提供了单独的ObservableCollection.我有一种方法,希望能够将ObservableCollection<Adventurer>ObservableCollection<Worker>都作为参数传递,到目前为止,我还没有碰到任何运气.该方法仅使用在IPerson中实现的属性,如果我将其声明为:

I have an interface IPerson and two classes, Adventurer and Worker that implement it. I currently have separate ObservableCollections for Adventurers and Workers. I have a method that I would like to be able to pass both an ObservableCollection<Adventurer> and an ObservableCollection<Worker> to as a parameter and so far I haven't had any luck. The method uses only properties implemented in IPerson and if I declare it as:

public void MyMethod(ObservableCollection<IPerson> collection)

...方法本身内部没有错误.但是,当我尝试传递ObservableCollection<Adventurer>时,出现2个错误:

...there are no errors inside the method itself. However, when I try to pass in an ObservableCollection<Adventurer>, I get 2 errors:

无法从"System.Collections.ObjectModel.ObservableCollection"转换为"System.Collections.ObjectModel.ObservableCollection"`

Cannot convert from 'System.Collections.ObjectModel.ObservableCollection' to 'System.Collections.ObjectModel.ObservableCollection'`

"AoW.MyMethod(System.Collections.ObjectModel.ObservableCollection)"的最佳重载方法匹配具有一些无效的参数.

The best overloaded method match for 'AoW.MyMethod(System.Collections.ObjectModel.ObservableCollection)' has some invalid arguments`

我是否可以将两个ObservableCollection传递给相同的方法?如果是这样,我应该怎么做?任何建议将不胜感激.

Is it possible for me to pass both ObservableCollections to the same method? If so, how should I go about it? Any advice would be greatly appreciated.

推荐答案

.NET中的类不支持协方差和相反方.只有接口可以.想象一下这种情况:

Classes in .NET don't support covariance and contravariance. Only interfaces do. Imagine this scenario:

class Adventurer : IPerson { }
class NPC : IPerson { }

public void MyMethod(ObservableCollection<IPerson> collection)
{
    collection.Add(new NPC());
}

var collectionOfAdventurers = new ObservableCollection<Adventurer>();
MyMethod(collectionOfAdventurers);

这显然会引起一些错误,因为传入的类型是Adventurer集合,但是该方法添加了NPC,但是在编译时无法验证.因此,编译器要求传递的类型必须与签名中的类型完全匹配,而不是允许这种不安全的行为,如果尝试传递任何其他内容,则会产生错误.

This would obviously raise some errors because the type passed in was an Adventurer collection, but the method added an NPC, but this cannot be verified at compile time. So rather than allow such unsafe behavior, the compiler requires that the type passed in must exactly match the type in the signature, and gives an error if you try to pass in anything else.

我建议将MyMethod更改为:

public void MyMethod(IEnumerable<IPerson> enumerable)

public void MyMethod(INotifyCollectionChanged collection)

具体取决于您需要访问的成员.

depending on exactly what members you need to access.

您可能还需要考虑一个通用方法:

You might also want to consider a generic method:

public void MyMethod<T>(ObservableCollection<T> collection) where T : IPerson

这篇关于ObservableCollection作为参数和接口传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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