// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.Linq;
using System.Reflection;
namespace Core.Common.Utils.Reflection
{
///
/// Utility class containing functions for retrieving specific information about a .net assembly.
///
public static class AssemblyUtils
{
///
/// Return attributes for a specific assembly
///
/// The assembly to read.
/// A structure containing all the assembly info provided.
public static AssemblyInfo GetAssemblyInfo(Assembly assembly)
{
AssemblyInfo info = new AssemblyInfo();
if (string.IsNullOrEmpty(assembly.Location))
{
return info;
}
info.Version = assembly.GetName().Version.ToString();
var assemblyTitleAttribute = GetAssemblyAttributeValue(assembly);
if (assemblyTitleAttribute != null)
{
info.Title = assemblyTitleAttribute.Title;
}
var assemblyDescriptionAttribute = GetAssemblyAttributeValue(assembly);
if (assemblyDescriptionAttribute != null)
{
info.Description = assemblyDescriptionAttribute.Description;
}
var assemblyProductAttribute = GetAssemblyAttributeValue(assembly);
if (assemblyProductAttribute != null)
{
info.Product = assemblyProductAttribute.Product;
}
var assemblyCopyrightAttribute = GetAssemblyAttributeValue(assembly);
if (assemblyCopyrightAttribute != null)
{
info.Copyright = assemblyCopyrightAttribute.Copyright;
}
var assemblyCompanyAttribute = GetAssemblyAttributeValue(assembly);
if (assemblyCompanyAttribute != null)
{
info.Company = assemblyCompanyAttribute.Company;
}
return info;
}
///
/// Return attributes for the current executing assembly.
///
/// A structure containing all the assembly info provided.
public static AssemblyInfo GetExecutingAssemblyInfo()
{
return GetAssemblyInfo(Assembly.GetExecutingAssembly());
}
///
/// Returns the type based on the full type name.
///
/// Full type name.
/// The matching the string name, null otherwise.
/// Specified type string is found in multiple assemblies.
public static Type GetTypeByName(string typeName)
{
Type result = null;
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
Type foundType = a.GetType(typeName);
if (foundType != null)
{
if (result == null)
{
result = foundType;
}
else
{
throw new AmbiguousMatchException(string.Format("Type '{0}' found in multiple assemblies", typeName));
}
}
}
return result;
}
///
/// Gets a to an embedded resource of an assembly.
///
/// The assembly from which to retrieve the embedded resource.
/// Name of the embedded file.
/// The byte-stream to the embedded resource.
/// Embedded resource file with name
/// cannot be found in .
public static Stream GetAssemblyResourceStream(Assembly assembly, string fileName)
{
try
{
return assembly.GetManifestResourceNames()
.Where(resourceName => resourceName.EndsWith(fileName))
.Select(assembly.GetManifestResourceStream)
.First();
}
catch (InvalidOperationException e)
{
var message = string.Format("Cannot find embedded resource file '{0}' in '{1}.",
fileName, assembly.FullName);
throw new ArgumentException(message, "fileName", e);
}
}
private static T GetAssemblyAttributeValue(Assembly assembly) where T : class
{
object[] attributes = assembly.GetCustomAttributes(typeof(T), true);
if (attributes.Length == 0)
{
return null;
}
return (T) attributes[0];
}
#region Nested type: AssemblyInfo
///
/// structure containing assembly attributes as strings.
///
/// Values will be null if they were not specified.
[Serializable]
public struct AssemblyInfo
{
///
/// Gets or sets the company specified in the assembly.
///
public string Company { get; set; }
///
/// Gets or sets the copyright text specified in the assembly.
///
public string Copyright { get; set; }
///
/// Gets or sets the description text specified in the assembly.
///
public string Description { get; set; }
///
/// Gets or sets the product text specified in the assembly.
///
public string Product { get; set; }
///
/// Gets or sets the title text specified in the assembly.
///
public string Title { get; set; }
///
/// Gets or sets the version specified in the assembly.
///
public string Version { get; set; }
}
#endregion
}
}