从.NET 4切换时,泛型停止编译. NET 3.5/Silverlight 4 [英] Generics stop compiling when switching from .NET 4 --> NET 3.5 / Silverlight 4
问题描述
大家好,
我正在将最初为.NET 4.0编写的大量c#代码放入silverlight 4应用程序中.当涉及继承时,编译器拒绝泛型时,我遇到了一个严重的问题.我在下面放了一个非常基本的示例:该代码在.NET 4中可以编译,但在Silverlight 4.0或.NET 3.5中不能.有经验的人可以告诉我这里发生了什么吗?我当然不能成为第一个遇到此类问题的人:
Hi everyone,
I''m in the process of putting a massive amount of c# code originally written for .NET 4.0 into a silverlight 4 app. I have quite a serious problem with the compiler rejecting generics when inheritance is involved. I''ve put a very basic example down below: this code compiles OK in .NET 4, but not in silverlight 4.0 or .NET 3.5. Can someone with more experience please tell me what''s going on here?? I surely can''t be the first to come across such issues:
using System;
using System.Collections.Generic;
namespace ClassLibrary2
{
public class Main<T>
where T : Class1
{
public void DoStuff()
{
//The following line compiles in .NET4 but does not compile in Silverlight 4
//Error 2 Argument 1: cannot convert from ''ClassLibrary2.ComparingClass'' to ''System.Collections.Generic.IComparer<T>''
Class2<T, IComparer<T>> returnVal = new Class2<T, IComparer<T>>(new ComparingClass());
}
}
public class Class1
{
}
class Class2<T, U>
where T : Class1
where U : IComparer<T>
{
internal Class2(U c)
{
}
}
class ComparingClass : IComparer<Class1>
{
public int Compare(Class1 a, Class1 b)
{
return 1;
}
}
}
从List< t>继承的东西也有类似的问题无法访问LINQ IEnumerable t扩展方法等.所有这些代码都在.NET 4中编译为A.OK ...
任何帮助将不胜感激.
我已经找到了解决方案*针对此示例*:
更改
I have similar problems with things inheriting from List<t> not getting access to LINQ IEnumerable<t> extension methods etc. All of this code compiled A-OK in .NET 4...
Any help will be greatly appreciated.
I''ve found a solution *to this example*:
change
class ComparingClass : IComparer<Class1>
到
to
class ComparingClass<T> : IComparer<T>
where T:Class1
但是总的来说,我不明白为什么编译器会以不同的方式对待它们.我有很多代码可以解决类似的问题,并且需要了解发生了什么问题才能对其进行修复.
but in general I don''t understand why the compiler thinks of them differently. I have a lot of code to move across with similar issues and need to understand what''s going wrong in order to fix it. Help!
推荐答案
这是预期的行为. .NET 4.0的IComparer<>
版本是互变的,请参见以下内容:
That''s expected behavior. .NET 4.0''s version ofIComparer<>
is contravariant, see below:
public interface IComparer<in T>
在.NET 3.5中,没有互变的概念.因此,在3.5中,您需要进行如下转换:
In .NET 3.5 there was no notion of contravariance. So in 3.5 you would need a cast as follows:
Class2<T, IComparer<T>> returnVal = new Class2<T, IComparer<T>>(
(IComparer<T>)new ComparingClass());
有关更多信息,请参阅我前一段时间做的博客条目:
C#4.0和变体通用接口 [ ^ ]
For more info, see a blog entry I did some time ago:
C# 4.0 and variant generic interfaces[^]
看起来像IComparer在版本4中引入,因此在较早版本中不可用.在这里看看:
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx [ ^ ]
祝你好运!
Looks like IComparer was introduced in version 4 and therefor not available in earlier versions. Have a look here:
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx[^]
Good luck!
更新:
如果其他人遇到类似问题,则从.NET 4切换到Silverlight:
我刚刚下载了Silverlight 5 Beta,发现这些问题"在该框架中不存在.升级!
Update:
In case anybody else is having similar issues switching to Silverlight from .NET 4:
I''ve just downloaded Silverlight 5 Beta and found that these ''problems'' do not exist in that framework. Upgrade!
这篇关于从.NET 4切换时,泛型停止编译. NET 3.5/Silverlight 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!