通用接口转换问题 [英] generic interface conversion question

查看:60
本文介绍了通用接口转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在项目中遇到问题,我已经提炼到以下

代码。不幸的是它不会编译并给出以下


错误(由于在HorseFarm的GetAnimals()方法中调用GetHorses()引起):

无法隐式转换类型''MyCollection< Horse>''到

''MyCollection< IAnimal>''

我尝试在Horse类中创建隐式转换运算符但

编译器告诉我,当转换为

或来自界面时这样做是违法的。

我可以''很清楚为什么转换不会自动发生,因为

Horse类实现了IAnimal所以你会认为一堆


匹马会是一个动物的集合(毕竟,只要有一个IAnimal,我就可以返回一个

马)。


话虽如此,我真正的实现很多更复杂,我真的需要在HorseFarm中利用相当于GetHorses()方法的
'这是

GetAnimals()方法。


是否有某种类型的仿制药设计模式可以解决这个问题?


谢谢,


戴夫

---------代码开始------------- -


公共课MyCollection< T>

{

}


公共界面IAnimal

{

}


公共接口IFarm

{

MyCollection< IAnimalGetAnimals();

}


公共类马:IAnimal

{

}


公共类HorseFarm:IFarm

{

MyCollection< Horsehorses = new MyCollection< Horse>();


public MyCollection< HorseGetHorses()

{

返回马匹;

}


public MyCollection< IAnimalGetAnimals()

{

返回GetHorses();

}

}


---------代码结束---------------

Hi all,

I''m having trouble with a project that I''ve distilled down to the following
code. Unfortunately it won''t compile and gives the following

error (caused by the call to GetHorses() in HorseFarm''s GetAnimals() method):
Cannot implicitly convert type ''MyCollection<Horse>'' to
''MyCollection<IAnimal>''
I tried creating an implicit conversion operator in the Horse class but the
compiler informed me that it was illegal to do so when the conversion was to
or from an interface.
I can''t quite see why the the conversion doesn''t occur automatically as the
Horse class implements IAnimal so you''d think a collection of

horses would be a collection of animals (I can after all return a single
Horse whenever an IAnimal is expected).

That being said, my real implementation is much more complex and I really
need to leverage the equivalent of a GetHorses() method within a HorseFarm''s
GetAnimals() method.

Is there some type of design pattern for generics that can solve this?

Thanks,

Dave
--------- CODE START ---------------

public class MyCollection<T>
{
}

public interface IAnimal
{
}

public interface IFarm
{
MyCollection<IAnimalGetAnimals();
}

public class Horse : IAnimal
{
}

public class HorseFarm : IFarm
{
MyCollection<Horsehorses = new MyCollection<Horse>();

public MyCollection<HorseGetHorses()
{
return horses;
}

public MyCollection<IAnimalGetAnimals()
{
return GetHorses();
}
}

--------- CODE END ---------------

推荐答案

Dave Weeden写道:
Dave Weeden wrote:

大家好,

我遇到了一个项目问题,我已经提炼到以下

代码。不幸的是它不会编译并给出以下


错误(由于在HorseFarm的GetAnimals()方法中调用GetHorses()引起):


无法将类型''MyCollection< Horse>''隐式转换为

''MyCollection< IAnimal>''

Hi all,

I''m having trouble with a project that I''ve distilled down to the following
code. Unfortunately it won''t compile and gives the following

error (caused by the call to GetHorses() in HorseFarm''s GetAnimals() method):
Cannot implicitly convert type ''MyCollection<Horse>'' to
''MyCollection<IAnimal>''



< ...>

Google c#covariance。

..Net(c#?)不支持协方差和逆变。


你应该能够将它定义为IAnimal [] GetAnimals()并在MyCollection上调用

ToArray()(假设你继承了List< T>) 。


JB

*除了在某些情况下。委托返回类型,数组转换...

<...>
Google c# covariance.
..Net (c#?) does not* support covariance and contravariance.

You should be able to define it as IAnimal[] GetAnimals() and call
ToArray() on MyCollection (presuming you''re inheriting List<T>).

JB
*Except in some cases. Delegate return types, Array conversion...


我不能完全理解为什么转换不会自动发生为
I can''t quite see why the the conversion doesn''t occur automatically as



马类实现了IAnimal,所以你会认为一堆动物是一组动物(毕竟,只要预期有IAnimal,我就可以返回一张

Horse。
the
Horse class implements IAnimal so you''d think a collection of

horses would be a collection of animals (I can after all return a single
Horse whenever an IAnimal is expected).



不,因为如果它是动物的集合你可以添加猪......

No, because if it was a collection of animals you could add a pig...


谢谢约翰,我现在正在挖掘以下内容:

http://blogs.msdn.com/ericlippert/ar...e/default.aspx


" John B"写道:
Thank John, I am now digging through the following:

http://blogs.msdn.com/ericlippert/ar...e/default.aspx

"John B" wrote:

Dave Weeden写道:
Dave Weeden wrote:

大家好,


我遇到了一个项目问题,我已经提炼到以下

代码。不幸的是它不会编译并给出以下


错误(由于在HorseFarm的GetAnimals()方法中调用GetHorses()引起):

无法将类型''MyCollection< Horse>''隐式转换为

''MyCollection< IAnimal>''
Hi all,

I''m having trouble with a project that I''ve distilled down to the following
code. Unfortunately it won''t compile and gives the following

error (caused by the call to GetHorses() in HorseFarm''s GetAnimals() method):
Cannot implicitly convert type ''MyCollection<Horse>'' to
''MyCollection<IAnimal>''



<。 ..>

Google c#covariance。

..Net(c#?)不支持协方差和逆变。


你应该能够将它定义为IAnimal [] GetAnimals()并在MyCollection上调用

ToArray()(假设你继承了List< T>)。

JB

*除某些情况外。委托返回类型,数组转换......

<...>
Google c# covariance.
..Net (c#?) does not* support covariance and contravariance.

You should be able to define it as IAnimal[] GetAnimals() and call
ToArray() on MyCollection (presuming you''re inheriting List<T>).

JB
*Except in some cases. Delegate return types, Array conversion...


这篇关于通用接口转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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