如何使用通用自定义集合实现多态接口

发布于 2024-11-09 08:57:11 字数 474 浏览 0 评论 0原文

好的 - 所以我想要完成的是:使用接受三种自定义类型之一(通过接口约束)的通用集合构建一个多接口。

问题就出现了

public virtual CustCollection<CustType1> GetEntities()
{
    return new CustCollection<CustType1>();
}

当我为And 编写一个实现,然后尝试在下一个取消级别覆盖它时, 。我什至无法选择覆盖此实现。

另一种可能性是使用接口,但是我需要接口接受泛型,而不是指定类型...我似乎无法正常工作,

即我需要的是:

interface IAccess<T>
{
    CustCollection<T> GetEntities();
}

但这似乎不是一个选项...

有什么建议吗?

ok - so what I want to accomplish is this: Build a poly interface making use of a generic collection that accepts one of three custom types (constrained through an interface).

The problem lies around when I write an implementation for

public virtual CustCollection<CustType1> GetEntities()
{
    return new CustCollection<CustType1>();
}

And then attempt to override it at the next level of derevation. It's not even an option for me to override this implementation.

The other possibility is making use of an interface, but then I need the interface to accept generic, not specify type...which I cannot seem to get working

i.e. what I need is:

interface IAccess<T>
{
    CustCollection<T> GetEntities();
}

but this is not an option it seems...

Any suggestions?

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

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

发布评论

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

评论(2

自找没趣 2024-11-16 08:57:11

我说得对吗? :)

public interface IUnknown { }

public class SoAndSo : IUnknown { }

public class CustomCollection<IUnknown>
{
}

public class Ancestor<T> where T : IUnknown
{
    public virtual CustomCollection<T> GetEntities()
    {
        return null;
    }
}

public class Descender : Ancestor<SoAndSo>
{
    public override CustomCollection<SoAndSo> GetEntities()
    {
        return base.GetEntities();
    }
}

Did I get you right? :)

public interface IUnknown { }

public class SoAndSo : IUnknown { }

public class CustomCollection<IUnknown>
{
}

public class Ancestor<T> where T : IUnknown
{
    public virtual CustomCollection<T> GetEntities()
    {
        return null;
    }
}

public class Descender : Ancestor<SoAndSo>
{
    public override CustomCollection<SoAndSo> GetEntities()
    {
        return base.GetEntities();
    }
}
手长情犹 2024-11-16 08:57:11

我最终创建了一个接口(如上所示的 IAccess),并在该接口上放置了类型约束...然后在与相关类型交互的所有类上使用该接口。

所以(抱歉,这是高水平的,我无法写出我的实际实现)

interface IAccess<T> where T : ITypeInterface
{
    CustCollection<T> GetEntities(Filter filter);
}

我尝试了建议的方法,但是我有一个由 3 个类组成的链,并且使用建议的模型与最顶层的父级存在问题。

我对自定义集合留下了约束:

 public class CustCollection<T> : IEnumerable<T> where T : ITypeInterface
    {}

这只对我有用,因为我意识到我真的不需要与父类共享功能(所以基本上没有最终强制执行真正的多态性......仅在公共对象的意义上)交互,核心功能是完全独立的)。

为了避免阴影,我声明 new

  public new CustCollection<Type> GetEntities(Filter filter)
    {
        return new CustCollection<Type>();
    }

我希望这对某些人有帮助......虽然我没有完全实现我一开始打算做的事情,但我发现这是适合我的情况的解决方法。

I ended up creating an interface (IAccess as shown above) and placed the constraint of type on that interface...then made use of that interface on all classes interacting with the types in question.

so (sorry this is high level, I can't write my actual implementation out)

interface IAccess<T> where T : ITypeInterface
{
    CustCollection<T> GetEntities(Filter filter);
}

I attempted what was suggested, but I have a chain of 3 classes inheriting and had issues with the top most parent using the suggested model.

I left the constraint on the custom collection:

 public class CustCollection<T> : IEnumerable<T> where T : ITypeInterface
    {}

This only really worked for me because I realized I really did not need to share functionality with the parent classes (so basically did not end up enforcing true polymorphism...only in the sense of common object interaction, core functionality is entirely seperate).

To avoid shadowing, I declare new

  public new CustCollection<Type> GetEntities(Filter filter)
    {
        return new CustCollection<Type>();
    }

I hope this is helpful to some...though I did not achieve exactly what I set out to do in the beginning, I found this to be a suitable workaround for my situation.

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