Question

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

Était-ce utile?

La solution

class TVIRoot<TLabelHandler> : OURTreeNodeImpl where TLabelHandler : SomeClass { } //yes

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top