Пример #1
0
        /// <summary>
        /// Deserializes an object from an assembly manifest resource.
        /// </summary>
        /// <typeparam name="TObject"> The run-time type of the object root to deserialize. </typeparam>
        /// <param name="serializationStrategy"> The serialization strategy. </param>
        /// <param name="resourceType"> A type within the source assembly where the manifest resource lives. </param>
        /// <param name="resourceName"> The fully qualified manifest resource name to load. </param>
        /// <param name="result"> A valid object of the specified type or null if the manifest resource name was not found in the assembly of the resource type. </param>
        /// <returns> A value indicating whether the manifest resource name was found in the target type's assembly. </returns>
        public static bool TryGetFromAssemblyResource <TObject>(this ICommonSerializationStrategy serializationStrategy, Type resourceType, string resourceName, out TObject result)
        {
            bool retval;

            if ((object)serializationStrategy == null)
            {
                throw new ArgumentNullException(nameof(serializationStrategy));
            }

            if ((object)resourceType == null)
            {
                throw new ArgumentNullException(nameof(resourceType));
            }

            if ((object)resourceName == null)
            {
                throw new ArgumentNullException(nameof(resourceName));
            }

            result = default(TObject);

            using (Stream stream = resourceType.GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
            {
                if (retval = ((object)stream != null))
                {
                    result = serializationStrategy.DeserializeObjectFromStream <TObject>(stream);
                }
            }

            return(retval);
        }
        /// <summary>
        /// Deserializes an object from an assembly manifest resource.
        /// </summary>
        /// <typeparam name="TObject"> The run-time type of the object root to deserialize. </typeparam>
        /// <param name="serializationStrategy"> The serialization strategy. </param>
        /// <param name="resourceType"> A type within the source assembly where the manifest resource lives. </param>
        /// <param name="resourceName"> The fully qualified manifest resource name to load. </param>
        /// <returns> A valid object of the specified type or null if the manifest resource name was not found in the assembly of the resource type. </returns>
        public static async ValueTask <TObject> GetFromAssemblyResourceAsync <TObject>(this ICommonSerializationStrategy serializationStrategy, Type resourceType, string resourceName)
        {
            TObject result;

            if ((object)serializationStrategy == null)
            {
                throw new ArgumentNullException(nameof(serializationStrategy));
            }

            if ((object)resourceType == null)
            {
                throw new ArgumentNullException(nameof(resourceType));
            }

            if ((object)resourceName == null)
            {
                throw new ArgumentNullException(nameof(resourceName));
            }

            result = default(TObject);

            await using (Stream stream = resourceType.GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
            {
                if ((object)stream != null)
                {
                    result = await serializationStrategy.DeserializeObjectFromStreamAsync <TObject>(stream);
                }
            }

            return(result);
        }