Пример #1
0
        /// <summary>
        /// Tries to load value from <paramref name="storage"/> associated with <paramref name="key"/> of <paramref name="type"/>.
        /// In default implementation uses underlaying <paramref name="storage"/> capability to load value of <paramref name="type"/>.
        /// </summary>
        /// <param name="storage">The composite storage to laod value from.</param>
        /// <param name="key">The key to value of.</param>
        /// <param name="type">The type of value to load.</param>
        /// <param name="value">The loaded value or <c>null</c>.</param>
        /// <returns><c>true</c> if <paramref name="value"/> was provided; <c>false</c> otherwise.</returns>
        protected virtual bool TryLoadValue(ICompositeStorage storage, string key, Type type, out object value)
        {
            MethodInfo methodInfo;

            if (!tryGetCache.TryGetValue(type, out methodInfo))
            {
                methodInfo = storage.GetType().GetMethods().FirstOrDefault(m => m.Name == tryGetMethodName && m.IsGenericMethod);
                if (methodInfo == null)
                {
                    value = null;
                    return(false);
                }

                methodInfo        = methodInfo.MakeGenericMethod(type);
                tryGetCache[type] = methodInfo;
            }

            object[] parameters = new object[2] {
                key, null
            };
            bool result = (bool)methodInfo.Invoke(storage, parameters);

            value = parameters[1];
            return(result);
        }
Пример #2
0
        /// <summary>
        /// Tries to load value from <paramref name="storage"/> associated with <paramref name="key"/> of <paramref name="type"/>.
        /// In default implementation uses underlaying <paramref name="storage"/> capability to load value of <paramref name="type"/>.
        /// </summary>
        /// <param name="storage">The composite storage to laod value from.</param>
        /// <param name="key">The key to value of.</param>
        /// <param name="type">The type of value to load.</param>
        /// <param name="value">The loaded value or <c>null</c>.</param>
        /// <returns><c>true</c> if <paramref name="value"/> was provided; <c>false</c> otherwise.</returns>
        protected virtual bool TryLoadValue(ICompositeStorage storage, string key, Type type, out object value)
        {
            MethodInfo methodInfo;

            if (!tryGetCache.TryGetValue(type, out methodInfo))
            {
                methodInfo = storage.GetType().GetMethods().FirstOrDefault(m => m.Name == tryGetMethodName && m.IsGenericMethod);
                if (methodInfo == null)
                {
                    value = null;
                    return(false);
                }

                methodInfo        = methodInfo.MakeGenericMethod(type);
                tryGetCache[type] = methodInfo;
            }

            try
            {
                object[] parameters = new object[2] {
                    key, null
                };
                bool result = (bool)methodInfo.Invoke(storage, parameters);

                value = parameters[1];
                return(result);
            }
            catch (TargetInvocationException e)
            {
                Console.WriteLine($"CTF: Key {key}, Type: {type.FullName}.");
                Console.WriteLine(e.InnerException.ToString());
                throw;
            }
        }