简体   繁体   English

C#。 是否可以使用具有基类型约束的静态泛型类,该基类型约束具有带有进一步基类型约束的方法

[英]C#. Is is possible to have a static generic class with a base type constraint that has a method with a further base type constraint

I'd like to be able to create a static generic type with a base type constraint like 我希望能够创建一个基类型约束的静态泛型类型

public static class Manager<T> where T : HasId
{
    public static T GetSingleById(ref List<T> items, Guid id)
    {
        // the Id is a property provided by HasId
        return (from i in items where i.Id == id select i).SingleOrDefault();
    }
}

Then add another method 然后添加另一种方法

...
    public static IEnumerable<T> GetManyByParentId(ref List<T> items, Guid parentId) where T : HasIdAndParentId
    {
        // the parentId is a property of HasIdAndParentId which subclasses HasId
        return from i in items where i.ParentId == parentId select i;
    }
...

Since HasIdAndParentId subclasses HasId the constraint T : HasId is met but the compiler won't accept the where base type constraint on the method. 由于HasIdAndParentId子类HasId满足约束条件T:HasId但编译器不接受方法的where基类型约束。

Any ideas? 有任何想法吗?

In this case, you're not redefining the type parameter on the method, so you can't apply any new constraints. 在这种情况下,您没有在方法上重新定义类型参数,因此您无法应用任何新约束。 You should be able to do it this way: 你应该能够这样做:

public static IEnumerable<T2> GetManyByParentId<T2>(
    ref List<T2> items, Guid parentId) 
    where T2 : T, HasIdAndParentId { .. } 

Make the GetManyByParentId method itself generic, and tie it's generic parameter to T : 使GetManyByParentId方法本身是通用的,并将其通用参数绑定到T

public static IEnumerable<R> GetManyByParentId<R>(
                                    ref List<R> items, Guid parentId) 
       where R : T, HasIdAndParentId 

Ben M's code sample will not compile unless HasIdAndParentId is an interface type, which it is not, judjing by the name. Ben M的代码示例将不会编译,除非HasIdAndParentId是一个接口类型,它不是,由名称判断。

Making the second method itself generic and making it depending on its own type parameter (distinct from T) will provide you the desired constraint. 使第二个方法本身具有通用性并使其依赖于自己的类型参数(与T不同)将为您提供所需的约束。

public static IEnumerable<T1> GetManyByParentId<T1>(ref List<T1> items, Guid parentId) where T1 : HasIdAndParentId
{
    // the parentId is a property of HasIdAndParentId which subclasses HasId
    return from i in items where i.ParentId == parentId select i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 new()和抽象基类的泛型类型约束 - Generic type constraint of new() and an abstract base class 如何在另一个通用基类上添加C#泛型类型约束? - How to add a C# generic type constraint on another generic base class? 定义方法采用IEnumerable-类型约束还是基类? - Defining method taking an IEnumerable - type constraint or base class? 具有接口作为类型约束的泛型类的c#扩展方法 - c# extension method for a generic class with interface as type constraint 构造/创建泛型类型并将类型约束转换为struct-as-base-type约束 - Constructing/making a generic type and turning a type constraint into a struct-as-base-type constraint 具有接口类型约束的C#泛型方法 - C# generic method with interface type constraint 使用泛型类型作为基类的静态成员 - Use generic type as a static member of base class c#泛型基类方法返回派生类的类型 - c# generic base class method return the type of the derived class 通用继承类的替代方法,用于将继承的属性约束为其基类型本身的子级 - Alternative to generic inherited class to constraint an inherited property to be a child of its base type itself 是否可能有一个基类,其返回类型可以调整为基类的类型? - Is it possible to have a base class with a return type that adjusts to the type of the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM