gpt4 book ai didi

c# - 接口(interface)的协方差问题导致来回转换

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:07 25 4
gpt4 key购买 nike

我一直在为我正在处理的项目使用 .NET 3.5。我遇到了一个小但烦人的协方差问题。这与我目前的模式类似:

public interface ISupportSomething<T> where T : struct
{
T StructProperty { get; set; }
}

public class SupportSomething : ISupportSomething<int>
{
public int StructProperty { get; set; }
}

public static class ISupportSomethingExtensions
{
public static IEnumerable<ISupportSomething<T>> Method<T>
(this IEnumerable<ISupportSomething<T>> collection)
{
return null;
}
}

public class SupportTester
{
private void SomeMethod()
{
IEnumerable<SupportSomething> source;

// invalid, i would generally fix this with a covariant
// declaration: ISupportSomething<out T>
var methodResult1 = source.Method();

// valid
var methodResult2 = source.Cast<ISupportSomething<int>>().Method();

// this means that if i want to make a function that
// returns an IEnumerable<SupportSomething> that
// has called ISupportSomethingExtensions.Method i
// have to do this cast back and forth approach

}

// here is a similar function to what i have that showcases the ugliness
// of calling this extension method
private IEnumerable<SupportSomething> SomeFunction()
{
IEnumerable<SupportSomething> source;

var tempSourceList = source.Cast<ISupportSomething<int>>();

tempSourceList = tempSourceList.Method();

return tempSourceList; // invalid
return tempSourceList.Cast<SupportSomething>(); //valid
}
}

我的问题很简单,我想我已经知道答案了,但是:在 .NET 3.5 中,有没有办法在处理这些对象时不必来回转换(参见最后一个函数)?

我猜我运气不好,不得不那样做,因为在 .NET 4.0 之前没有对协方差的通用支持。

最佳答案

您可以像这样修改扩展方法:

public static class ISupportSomethingExtensions
{
public static IEnumerable<T1> Method<T1, T2>
(this IEnumerable<T1> collection)
where T1 : ISupportSomething<T2>
where T2 : struct
{
return null;
}
}

现在它不需要依赖 IEnumerable 的协变性来工作。

此更改的不幸站点影响是 Method 将不再能够推断出它的通用参数,您需要明确列出:

private IEnumerable<SupportSomething> SomeFunction()
{
IEnumerable<SupportSomething> source = Enumerable.Empty<SupportSomething>();

return source.Method<SupportSomething, int>();
}

关于c# - 接口(interface)的协方差问题导致来回转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com