0

I would like to write a function with some tricky input arguments : This would be a param of types, and those type should implement a specific interface. So the signature would look like :

function void myFunction(string t,params Type[] types) where types:IMyInterface

Of course, it doesn't work like that.

Please don't suggest Generic Types, I expect several types, and I don't know how many.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Hermios
  • 622
  • 1
  • 5
  • 20
  • 1
    (string t,params IMyInterface[] types) – MajkeloDev Jul 26 '15 at 11:36
  • 1
    That's why interfaces where invented – MajkeloDev Jul 26 '15 at 11:36
  • @MajkeloDev That's not it. – Sergey Kalinichenko Jul 26 '15 at 11:37
  • 1
    What do You mean ? As far as I understand he want to accept Items which Implement IMyInterface interface. Am I missing something ? – MajkeloDev Jul 26 '15 at 11:38
  • Do you have an upper limit on the number of params? Can there be more than, say, ten of them? – Sergey Kalinichenko Jul 26 '15 at 11:38
  • @MajkeloDev He wants to accept `System.Type` objects which *represent* types derived from `IMyInterface`, not *instances* of `IMyInterface`. – Sergey Kalinichenko Jul 26 '15 at 11:40
  • duplicate? http://stackoverflow.com/questions/32664/is-there-a-constraint-that-restricts-my-generic-method-to-numeric-types?rq=1 – A G Jul 26 '15 at 11:42
  • It don't think it's possible. He can check in method if all the Types implements this inreface and Throw some exception if not – MajkeloDev Jul 26 '15 at 11:44
  • @MajkeloDev It's possible, but it's limited, and it does not look pretty (see below). – Sergey Kalinichenko Jul 26 '15 at 11:51
  • Aseem : You question is about numeric interface. Mine is on my own interface, nothing to do. dasblinkenlight : No limit. I want to develop a framework, so the more flexible as possible. MajkeloDev : Your last solution could work (I still need to do a cast later), but as noticed, this is really not pretty! – Hermios Jul 26 '15 at 12:04
  • 1
    @Hermios A great deal of very useful frameworks put a limit on the number of types. Take a look at `System.Func<...>` group of delegates, they are limited to 16 type parameters. The limit used to be four in .NET 3.5, and the framework was still pretty useful. – Sergey Kalinichenko Jul 26 '15 at 12:14

1 Answers1

0

You cannot force the compiler to do the compile-time check for you without using generics. Since generics do not provide variadic syntax for dealing with this kind of type parameter lists, a common workaround is to provide multiple overloads of a generic with different number of type parameters, and then call a common implementation:

void myFunction(string t) {
    myFunctionImpl(t, new Type[0]);
}
void myFunction<T0>(string t) where T0:IMyInterface {
    myFunctionImpl(t, new[] { typeof(T0) });
}
void myFunction<T0,T1>(string t) where T0:IMyInterface,
                                       T1:IMyInterface {
    myFunctionImpl(t, new[] { typeof(T0), typeof(T1) });
}
...
void myFunction<T0,T1,T2,T3,T4,T5,T6,T7,T8,T9>(string t)
    where T0:IMyInterface,
          T1:IMyInterface,
          T2:IMyInterface,
          T3:IMyInterface,
          T4:IMyInterface,
          T5:IMyInterface,
          T6:IMyInterface,
          T7:IMyInterface,
          T8:IMyInterface,
          T9:IMyInterface {
    myFunctionImpl(t, new[] { typeof(T0), typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9) });
}

This approach lets you call myFunction with up to ten type parameters as needed.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523