csharp/42skillz/Diverse/Diverse/Types/ReflectionExtensions.cs

ReflectionExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Diverse
{
    /// 
    /// Extension methods related to the usage of Reflection.
    /// 
    public static clast ReflectionExtensions
    {
        /// 
        /// Gets a value indicating whether a given  is already covered by the lib for Fuzzing.
        /// 
        /// The  to check.
        /// true if the  is already covered by the lib for Fuzzing, false otherwise.
        public static bool IsCoveredByAFuzzer(this Type type)
        {
            return Types.CoveredByAFuzzer.Contains(type);
        }

        /// 
        /// Gets a value indicating whether a given  is .
        /// 
        /// The  to check.
        /// true if the  is a  instance, false otherwise.
        public static bool IsEnumerable(this Type type)
        {
            return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable);
        }

        /// 
        /// Gets a value indicated whether a given  has no parameter defined.
        /// 
        /// The  to check absence of parameters for.
        /// true if the  has no parameter defined, false otherwise.
        public static bool IsEmpty(this ConstructorInfo constructor)
        {
            return constructor.GetParameters().Length == 0;
        }

        /// 
        /// Gets the constructor with the biggest number of Parameters of a .
        /// 
        /// The considered .
        /// The  which has the biggest number of Parameters defined for this .
        public static ConstructorInfo GetConstructorWithBiggestNumberOfParameters(this Type type)
        {
            var constructors = type.GetConstructorsOrderedByNumberOfParametersDesc().ToArray();

            if (!constructors.Any())
            {
                return null;
            }

            return constructors.First();
        }

        /// 
        /// Gets all the constructors of a  ordered by their number of parameters desc.
        /// 
        /// The considered .
        /// All the constructors of a  ordered by their number of parameters desc.
        public static IEnumerable GetConstructorsOrderedByNumberOfParametersDesc(this Type type)
        {
            var constructors = ((System.Reflection.TypeInfo)type).DeclaredConstructors;

            return constructors.OrderByDescending(c => c.GetParameters().Length);
        }
    }
}