I have a Type[] array and I want to get the Func type where T1, T2 etc... correspond to the types in the array. The array is not fixed in size but assume a type is available in the runtime (16 in .NET 4, 4 in .NET 3.5).
In .NET 4, I can do this and it works:
Type GetFuncType(Type typeRet, Type[] types)
{
return Type.GetType(string.Format("System.Func`{0}", types.Length + 1))
.MakeGenericType(types.Concat(new Type[] { typeRet } ).ToArray())
}
In .NET 3.5 however, the Type.GetType for the open generic type fails, returning NULL.
Is there a way I make this work in .NET 3.5? My only thought atm is to build up a string for the close generic type.
The preferred way of doing this is to use Expression.GetFuncType(Type[]) and Expression.GetActionType(Type[]). In the case of func, the last Type is the return, so:
Array.Resize(ref types, (types == null ? 0 : types.Length) + 1);
types[types.Length - 1] = typeRet;
return Expression.GetFuncType(types);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With