具有通用参数和抽象类的泛型

问题描述 投票:15回答:4

我有两个通用的基类。第二个泛型类对其第一个类的参数有约束。

abstract class FirstClass<T> {...}

abstract class SecondClass<U> where U : FirstClass {...}

这不起作用,因为没有定义FirstClass。所以我需要这样做。

abstract class FirstClass<T> {...}

abstract class SecondClass<U, T> where U : FirstClass<T> {...}

哪个有效。但是,这使得实现这些抽象类很难看。

class SomeClass {...}

class MyFirstClass : FirstClass<SomeClass> {...}

class MySecondClass : SecondClass<MyFirstClass, SomeClass> {...}

这对我来说似乎是多余的,因为我正在指定SomeClass两次。有没有办法以这样的方式声明它,即FirstClass中的T自动为SecondClass的U.我真正想要的是这样。

class SomeClass {...}

class MyFirstClass : FirstClass<SomeClass> {...}

class MySecondClass : SecondClass<MyFirstClass> {...}

虽然我怀疑这种确切的情况是可能的,但是有什么更清洁的我该做什么呢?

编辑

有几个人建议制作一流的界面。但我的定义更接近于此。

class FirstClass<T>
{
    public T MyObj { get; set; }
}

class SecondClass<U, T> where U : FirstClass<T>
{
    U MyFirstClass { get; set; }
}

使用接口,我无法从SecondClass访问MyFirstClass.MyObj。虽然我可以在IFirstClass上创建一个object T MyObj { get; set; },然后使用new隐藏它,如果我这样做,silverlight会在绑定中抛出一个契合。

c# silverlight generics
4个回答
4
投票

如果你实际上正在使用FirstClass的泛型类型参数(因为,从你的编辑中,它听起来像你),那么不,你正在寻找的东西是不可能的。编译器不区分相关的类型参数和不相关的类型参数。


11
投票

根据我的经验,最简单的方法是创建泛型类的非泛型接口。当您需要在不知道泛型类型的情况下强制转换为基类时,它也解决了这个问题。

interface IFirstClass {...}

abstract class FirstClass<T> : IFirstClass {...}

abstract class SecondClass<T> where T : IFirstClass {...}

2
投票

创建FirstClass实现的接口。然后,您可以将SecondClass约束到接口。


0
投票

这实际上是对Interface with two generic parameters, solve one automatically的回答,public interface IIntPersistentEntityService<TPersistentEntity> : IPersistentEntityService<TPersistentEntity, int> where TPersistentEntity : IPersistentEntity<int> { } public interface IStringPersistentEntityService<TPersistentEntity> : IPersistentEntityService<TPersistentEntity, string> where TPersistentEntity : IPersistentEntity<string> { } 被标记为此问题的副本。

您可以声明一组具有特定Id类型的接口。不是一个完美的解决方案,但简化了实体类的声明。

User

然后可以像这样声明public class UserService : IIntPersistentEntityService<User> { public User Get(int id) { throw new NotImplementedException(); } } 类:

qazxswpoi

如果您没有匹配的Id类型,则会出现编译器错误。

© www.soinside.com 2019 - 2024. All rights reserved.