views:

142

answers:

4

I'm doing something like this:

public abstract class FolderNode<TChildNode,TChildBusObj> : Node
    where TChildNode : MappedNode<TChildBusObj>, new()
    where TChildBusObj : new()
{
..
}

Is there a way of omitting TChildBusObj from the definition, but still be able access it in the code? E.g. I want to be able to infer the generic type of a generic type. I tried this:

public abstract class FolderNode<TChildNode> : Node
    where TChildNode : MappedNode<TChildBusObj>, new()
{
..
}

but I get this compile error:

CS0246: The type or namespace name 'TChildBusObj' could not be found (are you missing a using directive or an assembly reference?)

Update 22/01

I've actually determined I want this:

public class Folder<TChildNode, TBusObj> : MappedNode<TBusObj[]>
    where TChildNode : MappedNode<TBusObj>, new()

which it would be nice to shorten to this:

public class Folder<MappedNode<TBusObj>, TBusObj> : MappedNode<TBusObj[]>

But I can't because I get this error:

CS0081: Type parameter declaration must be an identifier not a type

I guess you cannot anonymize nested generic types in the class definition

A: 

Generic arguments can be specified either at the class name definition or at a method name definition.

Darin Dimitrov
+3  A: 

What would TChildBusObj refer to if you don't define what it refers to?

John Saunders
A: 

  Well, the thing is that you didn't speciefed the TChildBusObj.
   When compiled, the TChildBusObj, to be used as you want, will become something of a parameter.
   In the second implementation, there's no such declaration of the TChildBusObj being a generic parameter, so it assumes that the TChildBusObj is a type, and as you probably don't have any class named after that, it will throw a exception.
   Every generic parameter you use in the class definition have to be declared, so you can use it freely.

NoProblemBabe
A: 

why not just

 public class Folder<TBusObj> : MappedNode<TBusObj[]>
 {
     void Add(MappedNode<TBusObj> newChild) { ... }
 }

?

Ben Voigt