Пример #1
0
        /// <summary>
        /// Get the resolved resource type given its <paramref name="data"/>.
        /// </summary>
        /// <param name="data">The resource data.</param>
        /// <returns>The resource type.</returns>
        public Type GetResolvedType(IDictionary <string, object> data)
        {
            var resourceType = GetResolvedTypeInternal(data);

            if (!ResourceTypeResolverFactory.RequiresResolution(resourceType))
            {
                return(resourceType);
            }

            // If there is a more specific resolver available, resolve again recursively
            var moreSpecificResolver = ResourceTypeResolverFactory.CreateResolver(forType: resourceType);

            return(moreSpecificResolver.GetResolvedType(data));
        }
Пример #2
0
        /// <summary>
        /// Creates a new <see cref="Resource"/> from an existing dictionary.
        /// </summary>
        /// <typeparam name="T">The <see cref="Resource"/> type.</typeparam>
        /// <param name="existingDictionary">The existing dictionary.</param>
        /// <returns>The created <see cref="Resource"/>.</returns>
        public T CreateFromExistingData <T>(IDictionary <string, object> existingDictionary)
        {
            if (!ResourceTypeInfo.IsAssignableFrom(typeof(T).GetTypeInfo()))
            {
                throw new InvalidOperationException("Resources must inherit from the Resource class.");
            }

            var typeResolver = ResourceTypeResolverFactory.CreateResolver <T>();
            var resourceType = typeResolver.GetResolvedType(existingDictionary);

            var resource = Activator.CreateInstance(resourceType) as Resource;

            resource.Initialize(_client, this, existingDictionary, _logger);
            return((T)(object)resource);
        }