// Copyright (C) Stichting Deltares 2019. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.IO;
using System.Reflection;
namespace Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon.Assemblers
{
public static class AssemblyExtensions
{
///
/// Extracts an embedded file out of a given assembly.
///
/// The namespace of you assembly.
/// The name of the file to extract.
/// A stream containing the file data.
public static Stream GetEmbeddedFile(string assemblyName, string fileName)
{
Assembly assembly = Assembly.Load(assemblyName);
if (assembly == null)
{
throw new ArgumentException(string.Format("The assembly with name '{0}' couldnt be loaded",
assemblyName));
}
return assembly.GetEmbeddedFile(assemblyName + "." + fileName);
}
///
/// Extracts an embedded file out of a given assembly.
///
/// The assembly to extend
/// The name of the resource which normally is the namespace of you assembly and the file.
/// A stream containing the file data.
public static Stream GetEmbeddedFile(this Assembly assembly, string resourceName)
{
if (string.IsNullOrEmpty(resourceName) || resourceName.Trim() == "")
{
throw new ArgumentNullException("resourceName");
}
Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new EmbeddedResourceNotFoundException(assembly, resourceName);
}
return stream;
}
///
/// Extracts an embedded file out of a given assembly.
///
/// The type assembly to extend
/// The name of the resource which normally is the namespace of you assembly and the file.
/// A stream containing the file data.
public static Stream GetEmbeddedFile(this Type type, string resourceName, string folder = "Resources")
{
if (string.IsNullOrEmpty(resourceName) || resourceName.Trim() == "")
{
throw new ArgumentNullException("resourceName");
}
var assembly = type.Assembly;
var resource = string.Format("{0}.{1}.{2}", type.Namespace, folder, resourceName);
Stream stream = assembly.GetManifestResourceStream(resource);
if (stream == null)
{
throw new EmbeddedResourceNotFoundException(assembly, resourceName);
}
return stream;
}
///
/// Gets the resourcestring from the
///
/// The assembly to extend
/// The name of the embedded resource file
/// A string containing the contents of the file
public static string GetTextFromEmbeddedFile(this Assembly assembly, string resourceName)
{
using (var streamReader = new StreamReader(assembly.GetEmbeddedFile(resourceName)))
{
return streamReader.ReadToEnd();
}
}
}
}