Exemple #1
0
        /// <summary>
        /// Adds the specified property expression
        /// to the list of validated properties.
        /// </summary>
        /// <param name="expression">The property expression.</param>
        public void AddValidationProperty(Expression <Func <object> > expression)
        {
            PropertyInfo propertyInfo = ReflectionCompiler.GetPropertyInfo(expression);
            string       name         = propertyInfo.Name;

            MethodInfo    getMethodInfo = propertyInfo.GetMethod;
            Func <object> getter        = (Func <object>)getMethodInfo.CreateDelegate(
                typeof(Func <object>),
                this);

            AddValidationProperty(name, getter);
        }
Exemple #2
0
        void ResolveProperties(object instance, Dictionary <string, object> resolvedObjects)
        {
            Type type = instance.GetType();
            List <PropertyInfo> injectableProperties;

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterReadLock();
            }

            try
            {
                bool hasCachedInjectableProperties = injectablePropertyDictionary.TryGetValue(type, out injectableProperties);

                if (!hasCachedInjectableProperties)
                {
                    bool hasCachedProperties = propertyDictionary.TryGetValue(type,
                                                                              out PropertyInfo[] properties);

                    if (!hasCachedProperties)
                    {
#if NETSTANDARD || NETFX_CORE
                        properties = type.GetTypeInfo().DeclaredProperties.ToArray();
#else
                        properties = type.GetProperties();
#endif
                        propertyDictionary[type] = properties;
                    }

                    injectableProperties = new List <PropertyInfo>();

                    foreach (PropertyInfo propertyInfo in properties)
                    {
                        var dependencyAttributes = propertyInfo.GetCustomAttributes(typeof(InjectDependenciesAttribute), false);
#if NETSTANDARD || NETFX_CORE
                        if (!dependencyAttributes.Any())
#else
                        if (dependencyAttributes.Length < 1)                         /* Faster than using LINQ. */
#endif
                        {
                            continue;
                        }

                        injectableProperties.Add(propertyInfo);
                    }

                    injectablePropertyDictionary[type] = injectableProperties;
                }
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitReadLock();
                }
            }

            if (injectableProperties == null || injectableProperties.Count < 1)
            {
                return;
            }

            if (resolvedObjects == null)
            {
                resolvedObjects = new Dictionary <string, object>();
            }

            string typeName = type.FullName + ".";

            foreach (PropertyInfo propertyInfo in injectableProperties)
            {
                string fullPropertyName = typeName + propertyInfo.Name;

                object propertyValue = ResolveAux(propertyInfo.PropertyType, fullPropertyName, resolvedObjects);


                if (!propertyActionDictionary.TryGetValue(fullPropertyName,
                                                          out Action <object, object> setter))
                {
                    setter = ReflectionCompiler.CreatePropertySetter(propertyInfo);
                    propertyActionDictionary[fullPropertyName] = setter;
                }

                setter(instance, propertyValue);
            }
        }