Beispiel #1
0
        /// <summary>
        /// Initializes the default function injection.
        /// </summary>
        private static void InitializeDefaultFunctionInjection()
        {
            ParameterlessFunctionInjection <CultureInfo> injection = delegate()
            {
                return(_resourceHub?.DefaultCultureInfo);
            };

            GetCurrentCultureInfo = new ParameterlessPrioritizedFunctionInjection <CultureInfo>(injection);
        }
Beispiel #2
0
        /// <summary>
        /// Applies the injection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="staticType">Type of the static.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="injectionCandidate">The injection candidate.</param>
        /// <param name="expectAsLowPriority">if set to <c>true</c> [expect as low priority].</param>
        public static void ApplyInjection <T>(Type staticType, string propertyName, ParameterlessFunctionInjection <T> injectionCandidate, bool expectAsLowPriority = false)
        {
            if (staticType != null && !string.IsNullOrWhiteSpace(propertyName) && injectionCandidate != null)
            {
                var hitProperty = staticType.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty).FirstOrDefault(x => x.Name == propertyName);
                if (hitProperty != null)
                {
                    if (typeof(ParameterlessPrioritizedFunctionInjection <T>).IsAssignableFrom(hitProperty.PropertyType))
                    {
                        ParameterlessPrioritizedFunctionInjection <T> existed = hitProperty.GetValue(null) as ParameterlessPrioritizedFunctionInjection <T>;
                        if (existed == null)
                        {
                            if (hitProperty.SetMethod != null)
                            {
                                existed = new ParameterlessPrioritizedFunctionInjection <T>();
                                hitProperty.SetValue(null, existed);
                            }
                            else
                            {
                                throw ExceptionFactory.CreateInvalidObjectException(hitProperty?.Name, new { type = staticType?.FullName, property = hitProperty?.Name });
                            }
                        }

                        if (expectAsLowPriority)
                        {
                            existed.Append(injectionCandidate);
                        }
                        else
                        {
                            existed.Prepend(injectionCandidate);
                        }
                    }
                    else if (hitProperty.PropertyType == typeof(ParameterlessFunctionInjection <>).MakeGenericType(typeof(T)))
                    {
                        hitProperty.SetValue(null, injectionCandidate);
                    }
                }
            }
        }