Exemplo n.º 1
0
        /// <summary>
        /// Tests to see if a type has an attribute
        /// </summary>
        /// <param name="attributeType"></param>
        /// <param name="attributeFilter"></param>
        /// <returns></returns>
        public TypesThatConfiguration HaveAttribute(Type attributeType, Func <Attribute, bool> attributeFilter = null)
        {
            var notValue = GetNotAndingValue();
            Func <Type, bool> newFilter;

            if (attributeFilter != null)
            {
                var localFunc = attributeFilter;

                newFilter = t => t.GetTypeInfo().GetCustomAttributes(true).
                            Where(a => ReflectionService.CheckTypeIsBasedOnAnotherType(a.GetType(), attributeType)).
                            Any(a => localFunc((Attribute)a))
                            == notValue;
            }
            else
            {
                newFilter = t => t.GetTypeInfo().GetCustomAttributes(attributeType, true).
                            Any(a => ReflectionService.CheckTypeIsBasedOnAnotherType(a.GetType(), attributeType))
                            == notValue;
            }

            Add(newFilter);

            return(this);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Applies null check and disposal scope tracking logic to an expression
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="expression"></param>
        /// <param name="allowDisposableTracking"></param>
        /// <returns></returns>
        public static Expression ApplyNullCheckAndAddDisposal(IInjectionScope scope, IActivationExpressionRequest request, Expression expression, bool allowDisposableTracking)
        {
            if (expression.Type != request.ActivationType &&
                !ReflectionService.CheckTypeIsBasedOnAnotherType(expression.Type, request.ActivationType))
            {
                expression = Expression.Convert(expression, request.ActivationType);
            }

            if (!allowDisposableTracking)
            {
                if (request.DefaultValue != null)
                {
                    var method = typeof(ExpressionUtilities).GetRuntimeMethods()
                                 .FirstOrDefault(m => m.Name == "ValueOrDefault");

                    var closedMethod = method.MakeGenericMethod(request.ActivationType);

                    return(Expression.Call(closedMethod, expression, Expression.Constant(request.DefaultValue.DefaultValue, request.ActivationType)));
                }

                if (!scope.ScopeConfiguration.Behaviors.AllowInstanceAndFactoryToReturnNull &&
                    request.IsRequired)
                {
                    var closedMethod = CheckForNullMethodInfo.MakeGenericMethod(request.ActivationType);

                    return(Expression.Call(closedMethod,
                                           Expression.Constant(request.GetStaticInjectionContext()),
                                           expression));
                }

                return(expression);
            }
            if (request.DefaultValue != null)
            {
                var method = typeof(ExpressionUtilities).GetRuntimeMethods()
                             .FirstOrDefault(m => m.Name == "AddToDisposableScopeOrDefault");

                var closedMethod = method.MakeGenericMethod(request.ActivationType);

                return(Expression.Call(closedMethod, request.DisposalScopeExpression, expression, Expression.Constant(request.DefaultValue.DefaultValue, request.ActivationType)));
            }

            if (scope.ScopeConfiguration.Behaviors.AllowInstanceAndFactoryToReturnNull ||
                !request.IsRequired)
            {
                var closedMethod = AddToDisposalScopeMethodInfo.MakeGenericMethod(request.ActivationType);

                return(Expression.Call(closedMethod, request.DisposalScopeExpression, expression));
            }
            else
            {
                var closedMethod = CheckForNullAndAddToDisposalScopeMethodInfo.MakeGenericMethod(request.ActivationType);

                return(Expression.Call(closedMethod,
                                       request.DisposalScopeExpression,
                                       Expression.Constant(request.GetStaticInjectionContext()), expression));
            }
        }
        private bool BasedOnTypesFilter(Type type)
        {
            foreach (var basedOnType in _basedOnTypes)
            {
                if (ReflectionService.CheckTypeIsBasedOnAnotherType(type, basedOnType))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Tests for if one type is based on another
        /// </summary>
        /// <param name="injectionType"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        protected bool TestTypes(Type injectionType, Type[] types)
        {
            foreach (var type in types)
            {
                if (ReflectionService.CheckTypeIsBasedOnAnotherType(injectionType, type))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Filters types that are based on
        /// </summary>
        /// <param name="baseType"></param>
        /// <returns></returns>
        public TypesThatConfiguration AreBasedOn(Type baseType)
        {
            if (baseType == null)
            {
                throw new ArgumentNullException(nameof(baseType));
            }

            var notValue = GetNotAndingValue();

            Func <Type, bool> basedOnFilter =
                type => ReflectionService.CheckTypeIsBasedOnAnotherType(type, baseType) == notValue;

            Add(basedOnFilter);

            return(this);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a type filter that returns true if a type has a particular property name
        /// </summary>
        /// <param name="propertyType">property type</param>
        /// <param name="propertyName">property name</param>
        /// <returns>configuration object</returns>
        public TypesThatConfiguration HaveProperty(Type propertyType, string propertyName = null)
        {
            var notValue = GetNotAndingValue();

            if (propertyType == null)
            {
                Add(t => t.GetRuntimeProperties().Any(x => x.Name == propertyName) == notValue);
            }
            else
            {
                var tempType = propertyType;

                Add(t => t.GetRuntimeProperties().Any(
                        x => ReflectionService.CheckTypeIsBasedOnAnotherType(x.PropertyType, tempType) &&
                        (propertyName == null || x.Name == propertyName)) == notValue);
            }

            return(this);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Applies null check and disposal scope tracking logic to an expression
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="expression"></param>
        /// <param name="allowDisposableTracking"></param>
        /// <returns></returns>
        public static Expression ApplyNullCheckAndAddDisposal(IInjectionScope scope, IActivationExpressionRequest request, Expression expression, bool allowDisposableTracking)
        {
            if (expression.Type != request.ActivationType &&
                !ReflectionService.CheckTypeIsBasedOnAnotherType(expression.Type, request.ActivationType))
            {
                expression = Expression.Convert(expression, request.ActivationType);
            }

            if (!allowDisposableTracking)
            {
                if (!scope.ScopeConfiguration.Behaviors.AllowInstanceAndFactoryToReturnNull)
                {
                    var closedMethod = CheckForNullMethodInfo.MakeGenericMethod(request.ActivationType);

                    return(Expression.Call(closedMethod,
                                           Expression.Constant(request.GetStaticInjectionContext()),
                                           expression));
                }
            }
            else
            {
                if (scope.ScopeConfiguration.Behaviors.AllowInstanceAndFactoryToReturnNull)
                {
                    var closedMethod = AddToDisposalScopeMethodInfo.MakeGenericMethod(request.ActivationType);

                    return(Expression.Call(closedMethod, request.DisposalScopeExpression, expression));
                }
                else
                {
                    var closedMethod =
                        CheckForNullAndAddToDisposalScopeMethodInfo.MakeGenericMethod(request.ActivationType);

                    return(Expression.Call(closedMethod,
                                           request.DisposalScopeExpression,
                                           Expression.Constant(request.GetStaticInjectionContext()), expression));
                }
            }

            return(expression);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Tests to see if a type has an attribute
        /// </summary>
        /// <typeparam name="TAttribute"></typeparam>
        /// <param name="attributeFilter"></param>
        /// <returns></returns>
        public TypesThatConfiguration HaveAttribute <TAttribute>(Func <TAttribute, bool> attributeFilter = null)
            where TAttribute : Attribute
        {
            var notValue = GetNotAndingValue();
            Func <Type, bool> newFilter;

            if (attributeFilter != null)
            {
                newFilter = t => t.GetTypeInfo().GetCustomAttributes(true).
                            Where(a => ReflectionService.CheckTypeIsBasedOnAnotherType(a.GetType(), typeof(TAttribute))).
                            Any(
                    x =>
                {
                    var returnValue = false;
                    var attribute   =
                        x as TAttribute;

                    if (attribute != null)
                    {
                        returnValue = attributeFilter(attribute);
                    }

                    return(returnValue);
                })
                            == notValue;
            }
            else
            {
                newFilter = t => t.GetTypeInfo().GetCustomAttributes(typeof(TAttribute), true).
                            Any(a => ReflectionService.CheckTypeIsBasedOnAnotherType(a.GetType(), typeof(TAttribute)))
                            == notValue;
            }

            Add(newFilter);

            return(this);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Get a list of member injection info for a specific type
        /// </summary>
        /// <param name="type">type being activated</param>
        /// <param name="injectionScope">injection scope</param>
        /// <param name="request">request</param>
        /// <returns>members being injected</returns>
        public IEnumerable <MemberInjectionInfo> GetPropertiesAndFields(Type type, IInjectionScope injectionScope, IActivationExpressionRequest request)
        {
            foreach (var property in type.GetRuntimeProperties())
            {
                if (!property.CanWrite || !property.SetMethod.IsPublic || property.SetMethod.IsStatic)
                {
                    continue;
                }

                if (ReflectionService.CheckTypeIsBasedOnAnotherType(property.PropertyType, _memberType))
                {
                    if (_filter == null || _filter(property))
                    {
                        yield return(new MemberInjectionInfo {
                            MemberInfo = property, LocateKey = LocateKey, IsRequired = IsRequired
                        });
                    }
                }
            }

            foreach (var field in type.GetRuntimeFields())
            {
                if (!field.IsPublic || field.IsStatic)
                {
                    continue;
                }
                if (ReflectionService.CheckTypeIsBasedOnAnotherType(field.FieldType, _memberType))
                {
                    if (_filter == null || _filter(field))
                    {
                        yield return(new MemberInjectionInfo {
                            MemberInfo = field, LocateKey = LocateKey, IsRequired = IsRequired
                        });
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Get a list of member injection info for a specific type
        /// </summary>
        /// <param name="type">type being activated</param>
        /// <param name="injectionScope">injection scope</param>
        /// <param name="request">request</param>
        /// <returns>members being injected</returns>
        public IEnumerable <MemberInjectionInfo> GetPropertiesAndFields(Type type, IInjectionScope injectionScope, IActivationExpressionRequest request)
        {
            foreach (var property in type.GetRuntimeProperties())
            {
                if (!property.CanWrite || !property.SetMethod.IsPublic || property.SetMethod.IsStatic)
                {
                    continue;
                }

                if (ReflectionService.CheckTypeIsBasedOnAnotherType(property.PropertyType, _memberType))
                {
                    if (_filter == null || _filter(property))
                    {
                        var importAttribute = _processAttributes ?
                                              property.GetCustomAttributes().OfType <IImportAttribute>().FirstOrDefault() :
                                              null;

                        var importInfo = importAttribute?.ProvideImportInfo(property.PropertyType, property.Name);

                        object key = importInfo?.ImportKey ?? LocateKey;

                        if (key == null &&
                            injectionScope.ScopeConfiguration.Behaviors.KeyedTypeSelector(property.PropertyType))
                        {
                            key = property.Name;
                        }

                        yield return(new MemberInjectionInfo {
                            MemberInfo = property, LocateKey = key, IsRequired = importInfo?.IsRequired ?? IsRequired
                        });
                    }
                }
            }

            foreach (var field in type.GetRuntimeFields())
            {
                if (!field.IsPublic || field.IsStatic)
                {
                    continue;
                }
                if (ReflectionService.CheckTypeIsBasedOnAnotherType(field.FieldType, _memberType))
                {
                    if (_filter == null || _filter(field))
                    {
                        var importAttribute = _processAttributes ?
                                              field.GetCustomAttributes().OfType <IImportAttribute>().FirstOrDefault() :
                                              null;

                        var importInfo = importAttribute?.ProvideImportInfo(field.FieldType, field.Name);

                        object key = importInfo?.ImportKey ?? LocateKey;

                        if (key == null &&
                            injectionScope.ScopeConfiguration.Behaviors.KeyedTypeSelector(field.FieldType))
                        {
                            key = field.Name;
                        }

                        yield return(new MemberInjectionInfo {
                            MemberInfo = field, LocateKey = key, IsRequired = importInfo?.IsRequired ?? IsRequired
                        });
                    }
                }
            }
        }