使用提供的参数中指定的协变泛型类型调用方法

发布于 2024-10-24 04:34:01 字数 1290 浏览 5 评论 0原文

下面的 out 关键字(我不知道,但出于对您来说可能显而易见的原因)是不允许的:

public static class DataExtensions {
    public static void ReplaceAll<T>(this EntityCollection<T> collectionToReplace, IEnumerable<T> collectionToAdd) where T : EntityObject {
        RemoveEach(collectionToReplace);
        foreach (T item in collectionToAdd) collectionToReplace.Add(item);
    }

    public static void RemoveEach(this EntityCollection<out EntityObject> collectionToEmpty) {
        if (!collectionToEmpty.IsLoaded) collectionToEmpty.Load();
        while (collectionToEmpty.Any()) collectionToEmpty.Remove(collectionToEmpty.First());
    }
}

如果没有它,RemoveEach(collectionToReplace); 有一个参数不匹配:

参数 1:无法从“System.Data.Objects.DataClasses.EntityCollection”转换与 'System.Data.Objects.DataClasses.EntityCollection'

相同,当使用特定(非泛型)派生类型进行调用时。我必须使用以下签名吗?

public static void RemoveEach<T>(this EntityCollection<T> collectionToEmpty) where T : EntityObject {

如果是这样,智能感知或编译器应该警告我在此方法中使用抽象类作为通用类型说明符,因为我刚刚创建了一个不可调用的方法,不是吗?如果您不介意,请您指出为什么会这样(例如,如果允许的话,类型安全会被破坏或会导致混乱)。

谢谢你,香农

The out keyword below is (as I didn't know, but for reasons that may be obvious to you) not allowed:

public static class DataExtensions {
    public static void ReplaceAll<T>(this EntityCollection<T> collectionToReplace, IEnumerable<T> collectionToAdd) where T : EntityObject {
        RemoveEach(collectionToReplace);
        foreach (T item in collectionToAdd) collectionToReplace.Add(item);
    }

    public static void RemoveEach(this EntityCollection<out EntityObject> collectionToEmpty) {
        if (!collectionToEmpty.IsLoaded) collectionToEmpty.Load();
        while (collectionToEmpty.Any()) collectionToEmpty.Remove(collectionToEmpty.First());
    }
}

and without it RemoveEach(collectionToReplace); has a parameter mismatch:

Argument 1: cannot convert from 'System.Data.Objects.DataClasses.EntityCollection<T>' to 'System.Data.Objects.DataClasses.EntityCollection<System.Data.Objects.DataClasses.EntityObject>'

The same, when calling with a specific (non-generic) derived type. Do I have to do use the following signature?

public static void RemoveEach<T>(this EntityCollection<T> collectionToEmpty) where T : EntityObject {

If so, intellisense or the compiler should warn me about using an abstract class as the generic type-specifier in this method, because I've just created an un-callable method, no? And if you don't mind, would you please point out why this is so (e.g. a scenario where type-safety would be broken or confusion would result if it were allowed).

Thank you, Shannon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2024-10-31 04:34:01

您应该能够使用:

public static void RemoveEach<T>(this EntityCollection<T> collectionToEmpty)
    where T : class
{
    if (!collectionToEmpty.IsLoaded) collectionToEmpty.Load();
    while (collectionToEmpty.Any()) collectionToEmpty.Remove(collectionToEmpty.First());
}

这是 EntityCollection 需要的唯一约束,并且您没有在方法主体中使用需要 EntityObject 的任何内容。

顺便问一下,您在这里所做的事情与调用collectionToEmpty.Clear()有很大不同吗?我已经有一段时间没有使用 EF 了,所以这并不完全明显......

You should just be able to use:

public static void RemoveEach<T>(this EntityCollection<T> collectionToEmpty)
    where T : class
{
    if (!collectionToEmpty.IsLoaded) collectionToEmpty.Load();
    while (collectionToEmpty.Any()) collectionToEmpty.Remove(collectionToEmpty.First());
}

That's the only constraint that EntityCollection<T> requires, and you're not using anything within the method body which needs EntityObject.

By the way, is what you're doing here very different to calling collectionToEmpty.Clear()? I haven't used EF for a while, so it's not entirely obvious...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文