Skip to content

Commit

Permalink
Major code classes cloned - some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Jan 12, 2025
1 parent 9d5c1d2 commit ab86e64
Show file tree
Hide file tree
Showing 58 changed files with 5,083 additions and 131 deletions.
1 change: 1 addition & 0 deletions DNX.Extensions.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Populatable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=WORDIFY/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
257 changes: 257 additions & 0 deletions src/DNX.Extensions/Assemblies/AssemblyDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
using System;
using System.IO;
using System.Reflection;
using DNX.Extensions.Validation;

namespace DNX.Extensions.Assemblies;

/// <summary>
/// Implementation for obtaining Assembly Attributes
/// </summary>
/// <seealso cref="IAssemblyDetails" />
public class AssemblyDetails : IAssemblyDetails
{
#region Properties

/// <summary>
/// Internal Assembly field
/// </summary>
public Assembly Assembly { get; }

#endregion

#region Constructor(s)

/// <summary>
/// Initializes a new instance of the <see cref="AssemblyDetails" /> class.
/// </summary>
public AssemblyDetails()
: this(Assembly.GetCallingAssembly())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AssemblyDetails" /> class.
/// </summary>
/// <param name="assembly">The assembly.</param>
public AssemblyDetails(Assembly assembly)
{
Guard.IsNotNull(() => assembly);

Assembly = assembly;
}

#endregion

#region Internal Methods

/// <summary>
/// Returns the value of attribute T or String.Empty if no value is available.
/// </summary>
/// <typeparam name="T">The type of attribute to interrogate</typeparam>
/// <param name="getValue">The get value.</param>
/// <returns>The result of the specified Func, executed on the found attribute, if any.
/// <c>null</c> if not matching attribute can be found</returns>
protected string GetValue<T>(Func<T, string> getValue)
where T : Attribute
{
var a = (T)Attribute.GetCustomAttribute(Assembly, typeof(T));

return a == null
? null
: getValue(a);
}

#endregion

#region IAssemblyDetails Members

/// <summary>
/// Gets the AssemblyName
/// </summary>
/// <value>The name of the assembly.</value>
public AssemblyName AssemblyName
{
get { return Assembly.GetName(); }
}

/// <summary>
/// Gets the name of the assembly.
/// </summary>
/// <value>The name.</value>
public string Name
{
get { return AssemblyName.Name; }
}

/// <summary>
/// Gets the location of the assembly.
/// </summary>
/// <value>The location.</value>
public string Location
{
get { return Assembly.Location; }
}

/// <summary>
/// Gets the file name of the assembly.
/// </summary>
/// <value>The name of the file.</value>
public string FileName
{
get { return Path.GetFileName(Assembly.Location); }
}

/// <summary>
/// Gets the title attribute value.
/// </summary>
/// <value>The title.</value>
public string Title
{
get { return GetValue<AssemblyTitleAttribute>(a => a.Title); }
}

/// <summary>
/// Gets the product attribute value.
/// </summary>
/// <value>The product.</value>
public string Product
{
get { return GetValue<AssemblyProductAttribute>(a => a.Product); }
}

/// <summary>
/// Gets the copyright attribute value.
/// </summary>
/// <value>The copyright.</value>
public string Copyright
{
get { return GetValue<AssemblyCopyrightAttribute>(a => a.Copyright); }
}

/// <summary>
/// Gets the company attribute value.
/// </summary>
/// <value>The company.</value>
public string Company
{
get { return GetValue<AssemblyCompanyAttribute>(a => a.Company); }
}

/// <summary>
/// Gets the description attribute value.
/// </summary>
/// <value>The description.</value>
public string Description
{
get { return GetValue<AssemblyDescriptionAttribute>(a => a.Description); }
}

/// <summary>
/// Gets the trademark attribute value.
/// </summary>
/// <value>The trademark.</value>
public string Trademark
{
get { return GetValue<AssemblyTrademarkAttribute>(a => a.Trademark); }
}

/// <summary>
/// Gets the configuration attribute value.
/// </summary>
/// <value>The configuration.</value>
public string Configuration
{
get { return GetValue<AssemblyConfigurationAttribute>(a => a.Configuration); }
}

/// <summary>
/// Gets the assembly version.
/// </summary>
/// <value>The version.</value>
public Version Version
{
get { return AssemblyName.Version; }
}

/// <summary>
/// Gets or sets the simplified version.
/// </summary>
/// <value>The simplified version.</value>
public string SimplifiedVersion
{
get { return Version.Simplify(); }
}

/// <summary>
/// Gets the file version attribute value.
/// </summary>
/// <value>The file version.</value>
public string FileVersion
{
get { return GetValue<AssemblyFileVersionAttribute>(a => a.Version); }
}

/// <summary>
/// Gets the informational version attribute value.
/// </summary>
/// <value>The informational version.</value>
public string InformationalVersion
{
get { return GetValue<AssemblyInformationalVersionAttribute>(a => a.InformationalVersion); }
}

#endregion

#region Static Methods

/// <summary>
/// Creates an AssemblyDetails from the specified assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails ForAssembly(Assembly assembly)
{
return new AssemblyDetails(assembly);
}

/// <summary>
/// Creates an AssemblyDetails for the calling assembly
/// </summary>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails ForMe()
{
return ForAssembly(Assembly.GetCallingAssembly());
}

/// <summary>
/// Creates an AssemblyDetails for the entry point assembly.
/// </summary>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails ForEntryPoint()
{
return ForAssembly(Assembly.GetEntryAssembly());
}

/// <summary>
/// Creates an AssemblyDetails fors the assembly containing the specified Type
/// </summary>
/// <param name="type">The type.</param>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails ForAssemblyContaining(Type type)
{
return ForAssembly(type.Assembly);
}

/// <summary>
/// Creates an AssemblyDetails fors the assembly containing the specified Type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails ForAssemblyContaining<T>()
{
return ForAssembly(typeof(T).Assembly);
}

#endregion
}
71 changes: 66 additions & 5 deletions src/DNX.Extensions/Assemblies/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using DNX.Extensions.Reflection;

// ReSharper disable ConvertToUsingDeclaration

Expand All @@ -13,13 +16,71 @@ namespace DNX.Extensions.Assemblies;
public static class AssemblyExtensions
{
/// <summary>
/// Gets the embedded resource text.
/// Gets the assembly details.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <param name="relativeResourceName">Name of the relative resource.</param>
/// <param name="nameSpace">The name space.</param>
/// <returns></returns>
/// <exception cref="MissingManifestResourceException"></exception>
/// <returns>IAssemblyDetails.</returns>
public static IAssemblyDetails GetAssemblyDetails(this Assembly assembly)
{
return AssemblyDetails.ForAssembly(assembly);
}

/// <summary>
/// Find the types in the Assembly that implement the specified Type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assembly">The assembly.</param>
/// <returns>IList&lt;Type&gt;.</returns>
public static IList<Type> FindTypesThatImplementType<T>(this Assembly assembly)
{
var types = assembly.GetTypes()
.Where(t => t.IsA(typeof(T)))
.ToList();

return types;
}

/// <summary>
/// Find the concrete types in the Assembly that implement the specified Type
/// </summary>
/// <typeparam name="T">The type to find classes that implement</typeparam>
/// <param name="assembly">The assembly to search</param>
/// <returns>List of <see cref="Type"/></returns>
public static IList<Type> FindConcreteTypesThatImplementType<T>(this Assembly assembly)
{
var concreteClassTypes = FindTypesThatImplementType<T>(assembly)
.Where((t => t.IsClass && !t.IsAbstract))
.ToList();

return concreteClassTypes;
}

/// <summary>
/// Create instances of concrete types in an assembly that implement the specified type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assembly">The assembly to search</param>
/// <returns>List of instances of T></returns>
public static IList<T> CreateInstancesOfConcreteTypesThatImplementType<T>(this Assembly assembly)
{
var types = assembly.FindConcreteTypesThatImplementType<T>();

var instances = types
.Select(Activator.CreateInstance)
.Cast<T>()
.ToList();

return instances;
}

/// <summary>
/// Gets the embedded resource text.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <param name="relativeResourceName">Name of the relative resource.</param>
/// <param name="nameSpace">The name space.</param>
/// <returns></returns>
/// <exception cref="MissingManifestResourceException"></exception>
public static string GetEmbeddedResourceText(this Assembly assembly, string relativeResourceName, string nameSpace = null)
{
try
Expand Down
Loading

0 comments on commit ab86e64

Please sign in to comment.