質問

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

役に立ちましたか?

解決

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top