class TVIRoot<TLabelHandler> : OURTreeNodeImpl where TLabelHandler : SomeClass { } //yes
c# templates how can i apply constraints to a class that derives from something
-
29-03-2022 - |
質問
stoopid question time again.
I have this class that pulls in some code via a base class like so:
class TVIRoot : OURTreeNodeImpl { }
I now want to add some template functionality
class TVIRoot<TLabelHandler> : OURTreeNodeImpl { }
But I can't work out what sort of finger mangling I need to get it to compile when I need to supply some constraints.
class TVIRoot<TLabelHandler> where TLabelHandler : new(), OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> where TLabelHandler : SomeClass : OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> : OURTreeNodeImpl, where TLabelHandler : SomeClass { } //no
Can this be done?
Many thanks.
bg
解決
他のヒント
The constraint comes after the base class inheritance, here is an example:
public interface IFood
{
}
public class Animal
{
}
public class Cat<T> : Animal where T : IFood
{
public void Eat(T food)
{
}
}
for more details check: http://msdn.microsoft.com/en-US/library/d5x73970(v=vs.80).aspx
所属していません StackOverflow