Пример #1
0
        /// <summary>
        /// Loads the properties for the policy.
        /// </summary>
        private static void LoadPolicyProperties(CachePolicyConfigPolicy config, Type type, ICachePolicy policy)
        {
            if (config.Properties == null)
            {
                return;
            }

            foreach (var prop in config.Properties)
            {
                if (string.IsNullOrEmpty(prop.Name))
                {
                    throw new CachePolicyConfigException($"Policy '{config.Name}' has a Property with no Name.");
                }

                if (prop == null)
                {
                    continue;
                }

                try
                {
                    var pi = type.GetProperty(prop.Name) ?? throw new CachePolicyConfigException($"Policy '{config.Name}' Property '{prop.Name}' does not exist.");
                    if (prop.Value is string)
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            pi.SetValue(policy, prop.Value);
                        }
                        else
                        {
                            var pt = pi.PropertyType;
                            if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                pt = pi.PropertyType.GetGenericArguments()[0];
                            }

                            var mi   = pt.GetMethod("TryParse", new Type[] { typeof(string), pt.MakeByRefType() }) ?? throw new CachePolicyConfigException($"Policy '{config.Name}' Property '{prop.Name}' type must support TryParse.");
                            var args = new object[] { prop.Value, null };
                            if (!(bool)mi.Invoke(null, args))
                            {
                                throw new CachePolicyConfigException($"Policy '{config.Name}' Property '{prop.Name}' value is not valid.");
                            }

                            pi.SetValue(policy, args[1]);
                        }
                    }
                    else
                    {
                        pi.SetValue(policy, prop.Value);
                    }
                }
                catch (CachePolicyConfigException) { throw; }
                catch (Exception ex) { throw new CachePolicyConfigException($"Policy '{config.Name}' Property '{prop.Name}' is unable to be set: {ex.Message}", ex); }
            }
        }
Пример #2
0
        /// <summary>
        /// Loads the cache type and policy configurations.
        /// </summary>
        private static void LoadCaches(CachePolicyConfigPolicy config, ICachePolicy policy)
        {
            if (config.Caches == null)
            {
                return;
            }

            foreach (var cache in config.Caches)
            {
                if (!string.IsNullOrEmpty(cache))
                {
                    CachePolicyManager.Set(cache, policy);
                }
            }
        }