lundi 17 octobre 2016

How to create type of func with specified generic types?

I want to create Func type with specified generic type arguments, something like that like, but only with one statement

Type create_type(Type[] types)
{
   return typeof(Func<>).MakeGenericType(types); // error if types.Length != 1
   return typeof(Func<,>).MakeGenericType(types); // error if types.Length != 2
   return typeof(Func<,,>).MakeGenericType(types); // error if types.Length != 3
   return typeof(Func<,,,>).MakeGenericType(types); // error if types.Length != 4
// .... 
}

Of course, I can create auxuliary array:

var func_types = new Type[] { typeof(Func<>), typeof(Func<,>) , //...}

But this solution seems too ugly for me

Note: array of types can be long (up to 15) and I want to find elegant solution. Unfortunately, I am creating such type from meta data, written in another language





Aucun commentaire:

Enregistrer un commentaire