//****************************************************************************************************** // AssemblyExtensions.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 09/12/2008 - J. Ritchie Carroll // Generated original version of source code. // 08/07/2009 - Josh L. Patterson // Edited Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 09/28/2010 - Pinal C. Patel // Removed Debuggable extension method since its value source is no longer present in AssemblyInfo. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Reflection; using GSF.Diagnostics; namespace GSF.Reflection { /// /// Defines extension functions related to Assemblies. /// public static class AssemblyExtensions { /// /// Returns only assembly name and version from full assembly name. /// /// An to get the short name of. /// The assembly name and version from the full assembly name. public static string ShortName(this Assembly assemblyInstance) { return assemblyInstance.FullName.Split(',')[0]; } /// /// Gets the specified embedded resource from the assembly. /// /// Source assembly. /// The full name (including the namespace) of the embedded resource to get. /// The embedded resource. public static Stream GetEmbeddedResource(this Assembly assemblyInstance, string resourceName) { //Extracts and returns the requested embedded resource. return assemblyInstance.GetManifestResourceStream(resourceName); } /// /// Gets the title information of the assembly. /// /// Source assembly. /// The title information of the assembly. public static string Title(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Title; } /// /// Gets the description information of the assembly. /// /// Source assembly. /// The description information of the assembly. public static string Description(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Description; } /// /// Gets the company name information of the assembly. /// /// Source assembly. /// The company name information of the assembly. public static string Company(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Company; } /// /// Gets the product name information of the assembly. /// /// Source assembly. /// The product name information of the assembly. public static string Product(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Product; } /// /// Gets the copyright information of the assembly. /// /// Source assembly. /// The copyright information of the assembly. public static string Copyright(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Copyright; } /// /// Gets the trademark information of the assembly. /// /// Source assembly. /// The trademark information of the assembly. public static string Trademark(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Trademark; } /// /// Gets the configuration information of the assembly. /// /// Source assembly. /// The configuration information of the assembly. public static string Configuration(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Configuration; } /// /// Gets a boolean value indicating if the assembly has been built as delay-signed. /// /// Source assembly. /// true, if the assembly has been built as delay-signed; otherwise, false. public static bool DelaySign(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).DelaySign; } /// /// Gets the version information of the assembly. /// /// Source assembly. /// The version information of the assembly public static string InformationalVersion(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).InformationalVersion; } /// /// Gets the name of the file containing the key pair used to generate a strong name for the attributed assembly. /// /// Source assembly. /// A string containing the name of the file that contains the key pair. public static string KeyFile(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).KeyFile; } /// /// Gets the culture name of the assembly. /// /// Source assembly. /// The culture name of the assembly. public static string CultureName(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).CultureName; } /// /// Gets the assembly version used to instruct the to ask for /// a particular version of a satellite assembly to simplify updates of the main assembly of an application. /// /// Source assembly. /// The satellite contract version of the assembly. public static string SatelliteContractVersion(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).SatelliteContractVersion; } /// /// Gets the string representing the assembly version used to indicate to a COM client that all classes in the /// current version of the assembly are compatible with classes in an earlier version of the assembly. /// /// Source assembly. /// The string representing the assembly version in MajorVersion.MinorVersion.RevisionNumber.BuildNumber format. public static string ComCompatibleVersion(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).ComCompatibleVersion; } /// /// Gets a boolean value indicating if the assembly is exposed to COM. /// /// Source assembly. /// true, if the assembly is exposed to COM; otherwise, false. public static bool ComVisible(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).ComVisible; } /// /// Gets a boolean value indicating if the was built in debug mode. /// /// Source assembly. /// true, if the assembly was built in debug mode; otherwise, false. public static bool Debuggable(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).ComVisible; } /// /// Gets the assembly GUID that is used as an ID if the assembly is exposed to COM. /// /// Source assembly. /// The assembly GUID that is used as an ID if the assembly is exposed to COM. public static string Guid(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).Guid; } /// /// Gets the string representing the assembly version number in MajorVersion.MinorVersion format. /// /// Source assembly. /// The string representing the assembly version number in MajorVersion.MinorVersion format. public static string TypeLibVersion(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).TypeLibVersion; } /// /// Gets a boolean value indicating whether the indicated program element is CLS-compliant. /// /// Source assembly. /// true, if the program element is CLS-compliant; otherwise, false. public static bool CLSCompliant(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).CLSCompliant; } /// /// Gets the date and time when the assembly was last built. /// /// Source assembly. /// The date and time when the assembly was last built. public static DateTime BuildDate(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).BuildDate; } /// /// Gets the root namespace of the assembly. /// /// Source assembly. /// The root namespace of the assembly. public static string RootNamespace(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).RootNamespace; } /// /// Gets a name/value collection of assembly attributes exposed by the assembly. /// /// Source assembly. /// A NameValueCollection of assembly attributes. public static NameValueCollection GetAttributes(this Assembly assemblyInstance) { return new AssemblyInfo(assemblyInstance).GetAttributes(); } /// /// Recursively attempts to load all assemblies referenced from the given assembly. /// /// The assembly whose references are to be loaded. /// true if the references were successfully loaded; false otherwise. /// /// If an object is created from a type that is loaded from an assembly, and if that /// assembly's references fail to load during instantiation, an exception may be thrown /// from both the constructor and the finalizer of the object that was instantiated. /// This method allows us to ensure that all referenced assemblies can be loaded /// before attempting to instantiate a type from that assembly. /// public static bool TryLoadAllReferences(this Assembly assemblyInstance) { return TryLoadAllReferences(assemblyInstance, new HashSet(AppDomain.CurrentDomain.GetAssemblies().Select(assembly => assembly.GetName().ToString()))); } // Recursively attempts to load all assemblies referenced from the given assembly. private static bool TryLoadAllReferences(Assembly assembly, ISet validNames) { using (Logger.SuppressFirstChanceExceptionLogMessages()) { try { // Base case: all referenced assemblies' names are present in the set of valid names IEnumerable referencedAssemblyNames = assembly.GetReferencedAssemblies() .Where(referencedAssemblyName => !validNames.Contains(referencedAssemblyName.ToString())); // Load each referenced assembly and recursively load their references as well foreach (AssemblyName referencedAssemblyName in referencedAssemblyNames) { Assembly referencedAssembly = Assembly.Load(referencedAssemblyName); validNames.Add(referencedAssemblyName.ToString()); if (!TryLoadAllReferences(referencedAssembly, validNames)) return false; } // All referenced assemblies loaded successfully return true; } catch (Exception ex) { Logger.SwallowException(ex, "AssemblyExtensions.cs TryLoadAllReferences: Failed to load referenced assembly."); return false; } } } } }