1

I want something like this:

public static TTo JumpTo<TFrom, TTo>(this TFrom from_page) 
       where TTo : new() TFrom : new()
{
    ...
}

And I want to enforce that TFrom and TTo are both derived from a base type.

And I want to make this method as an extension method of TFrom type.

Is it possible ? And what's the correct syntax?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 2
    @AD.Net: And your point with that demand? Edit: and that wont compile. – leppie Aug 27 '12 at 14:28
  • Btw the, the `new` constraint on the input parameter does not really make sense, unless you copying stuff internally. – leppie Aug 27 '12 at 14:33
  • @AD.Net: That sounds more like VB.NET except in VB.NET it would be `Of TTo As SomeType, TFrom As SomeOtherType` so actually not at all – Ry- Aug 27 '12 at 14:35
  • you guys are right, got my syntax wrong! – AD.Net Aug 27 '12 at 14:38

1 Answers1

10

Put the keyword where before each type.

public static TTo JumpTo<TFrom, TTo>(this TFrom from_page) 
    where TTo : SomeBaseType, new() 
    where TFrom : SomeOtherBaseType, new()
{
     ...
}
cadrell0
  • 17,109
  • 5
  • 51
  • 69