Skip to main content
 首页 » 编程设计

c#之我很难将我的 IEnumerableT> 实现切换到 ICollectionT>

2024年02月13日13yyy_WW

这让我发疯,所以我想我应该把它扔给社区,而不是用头撞 table 。

我正在将我的应用程序的一个区域从使用 IEnumerable 切换到 ICollection。 我在 IEnumerableOfIInterfaceExtensions 中定义了几个扩展方法,我想将其转换为 ICollectionOfIInterfaceExtensions,在我将所有内容都关闭后,但是当我关闭我的存储库中的集合时,我得到了(意外的)错误:

System.Collections.Generic.ICollection<T>' does not contain a definition for 'Bar' and the best extension method overload 'Application.Data.Repositories.RepositoryExtensions.Bar(System.Collections.Generic.ICollection<Application.Domain.Models.IInterface>, System.Collections.Generic.IEnumerable<Application.Domain.Models.IInterface>)' has some invalid arguments    

这仅在我尝试使用 ICollection 而不是 IEnumerable 时弹出。 我使用 IEnumerable 的实现

[编辑 为了清楚起见,我已经更新以添加我的类定义。 IDataRepository 包含一个返回 IEnumerable 的方法“Retrieve”,IEntityRepository 包含一个接受 IInterface 的无效方法“Save”

public class Repository<T>: IDataRepository<T>,IEntityRepository<T> 
                        where T: class, IInterface 
{ 
    protected virtual IEnumerable<T> Collection{ get; set;} 
    public virtual void Bar(T thing) 
    { 
        try 
        { 
            if (Collection == null) return; 
            Collection = (IEnumerable<T>)Collection.Foo(thing); 
 
        } 
        catch(Exception ex) 
        { 
            //breakpoint for debugging 
        } 
    } 
} 

Foo 的签名为 IEnumerable

internal static class MyExtensions 
{ 
    internal static IEnumerable<IInterface> Foo(this ICollection<IInterface> c1, 
                                                    IEnumerable<IInterface> c2) 
    { 
        //implementation removed for posting 
        return c1; 
    } 
internal static IEnumerable<IInterface> Foo(this IEnumerable<IInterface> c1, 
                                                    IInterface thing) 
    { 
        return Foo(c1, new List<IInterface>() { thing }); 
    } 
} 

作为 IEnumerable,CodeLens 报告每个 Foo 都有 1 个引用。客户端调用环绕第一个的第二个。每当我将它们切换到 ICollection 时,我在 CodeLens 中的引用就会消失,并且我会收到上面发布的异常...这毫无意义,因为在抛出异常时我在 ICollection 上拥有扩展方法.这是使用 ICollections 的 Foo 接口(interface)

protected virtual ICollection<T> Collection { get; set;} 
    public virtual void Bar(T thing) 
    { 
        try 
        { 
            if (Collection == null) return; 
            Collection = (ICollection<T>)Collection.Foo(entity); 
 
        } 
        catch(Exception ex) 
        { 
            //breakpoint 
        } 
    } 
 
internal static class MyExtensions 
{ 
    internal static ICollection<IInterface> Foo(this ICollection<IInterface> c1, 
                                                    IEnumerable<IInterface> c2) 
    { 
        //implementation removed for posting 
        return c1; 
    } 
    internal static ICollection<IInterface> Foo(this ICollection<IInterface> c1, 
                                                    IInterface thing) 
    { 
        return Foo(cache, new List<IInterface>() { thing }); 
    } 
} 

谁能确定将其从 IEnumerable 切换到 ICollection 时出现的问题?

请您参考如下方法:

这是协方差问题。 <强> IEnumerable<T>是协变的, ICollection<T>不是。

public interface IEnumerable<out T> : IEnumerable 
public interface ICollection<T> : IEnumerable<T>, IEnumerable 

你可以看到 out关键字仅在 IEnumerable<T> 中声明。

这就是为什么您的扩展方法仅适用于 ICollection<IInterface> 的原因, 而不是 ICollection<ClassWhichImplementsIInterface> .

使您的扩展方法通用以使其工作:

internal static class MyExtensions 
{ 
    internal static ICollection<T> Foo<T>(this ICollection<T> c1, IEnumerable<T> c2) where T : IInterface 
    { 
        //implementation removed for posting 
        return c1; 
    } 
    internal static ICollection<T> Foo<T>(this ICollection<T> c1, T thing) where T : IInterface 
    { 
        return Foo(cache, new List<T>() { thing }); 
    } 
} 

或

internal static class MyExtensions 
{ 
    internal static ICollection<IInterface> Foo<T>(this ICollection<IInterface> c1, IEnumerable<T> c2) where T : IInterface 
    { 
        //implementation removed for posting 
        return c1; 
    } 
    internal static ICollection<IInterface> Foo<T>(this ICollection<T> c1, T thing) where T : IInterface 
    { 
        return Foo(cache, new List<IInterface>() { thing }); 
    } 
} 

取决于您希望它返回什么:ICollection<IInterface>或 ICollection<T> .