c#模板如何将约束应用于从某个对象派生的类

c#模板如何将约束应用于从某个对象派生的类,c#,generics,generic-constraints,C#,Generics,Generic Constraints,我又问了一次 我有一个类,它通过一个基类引入一些代码,如下所示: class TVIRoot : OURTreeNodeImpl { } 现在我想添加一些模板功能 class TVIRoot<TLabelHandler> : OURTreeNodeImpl { } 类TVIRoot:OURTreeNodeImpl{} 但是,当我需要提供一些约束条件时,我无法确定需要什么样的手指损伤来编译它 class TVIRoot<TLabelHandler> where TLa

我又问了一次

我有一个类,它通过一个基类引入一些代码,如下所示:

class TVIRoot : OURTreeNodeImpl { }
现在我想添加一些模板功能

class TVIRoot<TLabelHandler> : OURTreeNodeImpl { }
类TVIRoot:OURTreeNodeImpl{}
但是,当我需要提供一些约束条件时,我无法确定需要什么样的手指损伤来编译它

class TVIRoot<TLabelHandler> where TLabelHandler : new(), OURTreeNodeImpl { } //no    
class TVIRoot<TLabelHandler> where TLabelHandler : SomeClass : OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> : OURTreeNodeImpl, where TLabelHandler : SomeClass { } //no
类TVIRoot,其中TLabelHandler:new(),OURTreeNodeImpl{}//no
类TVIRoot,其中TLabelHandler:SomeClass:OURTreeNodeImpl{}//no
类TVIRoot:OURTreeNodeImpl,其中TLabelHandler:SomeClass{}//no
这能做到吗

非常感谢

bg

类TVIRoot:OURTreeNodeImpl其中TLabelHandler:SomeClass{}//yes

约束位于基类继承之后,下面是一个示例:

public interface  IFood
{
}

public class Animal
{
}

public class Cat<T> : Animal where T : IFood
{
    public void Eat(T food)
    {
    }
}
公共接口IFood
{
}
公营动物
{
}
公共类猫:动物,T:I食物
{
公共食品(T食品)
{
}
}
有关更多详细信息,请查看:

public interface  IFood
{
}

public class Animal
{
}

public class Cat<T> : Animal where T : IFood
{
    public void Eat(T food)
    {
    }
}