示例#1
0
        public static bool IsTuple(this Type type)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            return(typeof(ITuple).IsAssignableFrom(type));
        }
示例#2
0
        public static bool IsNullable([NotNull] this Type type)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            return(Nullable.GetUnderlyingType(type) is object);
        }
示例#3
0
        public static bool HasDefaultConstructor([NotNull] this Type type)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            return(ConstructorCache.GetOrAddFor(type));
        }
示例#4
0
        public static bool IsStatic([NotNull] this Type type)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            return(type.IsAbstract && type.IsSealed);
        }
示例#5
0
        /// <summary>
        /// Creates the mapper for the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        public Mapper([NotNull] Type type)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            _source = type;
        }
示例#6
0
        public static bool TryGetCustomAttribute <T>(
            [NotNull] this Type type,
            [MaybeNullWhen(false)] out T attribute,
            bool inherited = true) where T : Attribute
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            attribute = type.GetCustomAttribute <T>(inherited);
            return(attribute is object);
        }
示例#7
0
        public object CreateInstance([NotNull] params object[] args)
        {
            if (args is null)
            {
                Throw.NullArgument(nameof(args));
            }

            if (_target.IsAbstract)
            {
                Throw.InvalidOperation("You cannot create an instance of an abstract type.");
            }

            if (args.Length != Parameters.Count)
            {
                Throw.ParameterCountMismatch($"This constructor {_target.Name}({string.Join(", ", Parameters.Select(x => x.ParameterType.Name))}) requires these parameters to be used.");
            }

            for (var index = 0; index < args.Length; index++)
            {
                if (!args[index].GetType().CanBeConvertedTo(Parameters[index].ParameterType))
                {
                    Throw.InvalidOperation($"The type {args[index].GetType().Name} cannot be implicitly converted to {Parameters[index].ParameterType.Name}");
                }
            }

            if (_constructor is object)
            {
                return(_constructor(args));
            }

            var array = Expression.Parameter(typeof(object[]));

            var parameters = args
                             .Zip(Enumerable.Range(0, args.Length),
                                  (arg, index) => Expression.Convert(
                                      Expression.ArrayIndex(
                                          array,
                                          Expression.Constant(index)),
                                      arg.GetType()))
                             .ToArray();
            var @new    = Expression.New(Member, parameters);
            var convert = Expression.Convert(@new, typeof(object));

            _constructor = Expression.Lambda <Func <object[], object> >(convert, array).Compile();

            return(_constructor(args));
        }
示例#8
0
        /// <summary>
        /// Indicates if the provided type implements the provided interface type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="interfaceType">The type of the interface to check.</param>
        /// <returns></returns>
        public static bool Implements([NotNull] this Type type, [NotNull] Type interfaceType)
        {
            if (type is null)
            {
                Throw.NullArgument(nameof(type));
            }

            if (interfaceType is null)
            {
                Throw.NullArgument(nameof(interfaceType));
            }

            if (type.IsInterface || !interfaceType.IsInterface)
            {
                return(false);
            }

            return(interfaceType.IsAssignableFrom(type));
        }
示例#9
0
        /// <summary>
        /// Indicates if the provided type inherits from the base type provided.
        /// </summary>
        /// <param name="derived">The derived type.</param>
        /// <param name="base">The potential base of the type.</param>
        /// <returns></returns>
        public static bool Inherits([NotNull] this Type derived, [NotNull] Type @base)
        {
            if (derived is null)
            {
                Throw.NullArgument(nameof(derived));
            }

            if (@base is null)
            {
                Throw.NullArgument(nameof(@base));
            }

            if (derived == @base)
            {
                return(false);
            }

            if (derived.IsInterface)
            {
                return(@base.IsInterface && derived.GetTypeInfo().ImplementedInterfaces.Any(i => i == @base));
            }

            return(@base.IsAssignableFrom(derived));
        }