Skip to content
Snippets Groups Projects
FuncExtensions.cs 1.32 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class FuncExtensions
    {
    
        public static readonly Type[] GenericFuncTypes = new Type[]
    
    {
        //typeof(Action),
        typeof(Func<>),
        typeof(Func<,>),
        typeof(Func<,,>),
        typeof(Func<,,,>),
        typeof(Func<,,,,>),
        typeof(Func<,,,,,>),
        typeof(Func<,,,,,,>),
    
        typeof(Func<,,,,,,,>),
        typeof(Func<,,,,,,,,>),
        typeof(Func<,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,,,,,>),
        typeof(Func<,,,,,,,,,,,,,,,,>),
    
    };
    
        private static readonly IReadOnlyDictionary<Type, int> GenericFuncSignature =
            GenericFuncTypes.ToDictionary(t => t, t => t.IsGenericType ? t.GetGenericArguments().Count() : 0);
    
        public static Type GetGenericFuncType(int signatureCount)
            => GenericFuncTypes[signatureCount - 1];
    
        public static bool IsFuncType(Type type, out int signatureCount)
            => GenericFuncSignature.TryGetValue(
                    type.IsGenericType ? type.GetGenericTypeDefinition() : type,
                    out signatureCount
            );
    
        public static Type CreateFuncType(Type[] signature)
            => GetGenericFuncType(signature.Length)
                .MakeGenericType(signature);
    }