Skip to content

Commit

Permalink
feat: Add Assembly and Reflection helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Jun 6, 2021
1 parent 0badda1 commit e0d6adf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Shared/Helpers/AssemblyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Concurrent;
using System.Reflection;

namespace Armory.Shared.Helpers
{
public static class AssemblyHelper
{
private static readonly ConcurrentDictionary<string, Assembly> Assemblies = new();

public static Assembly GetInstance(string key)
{
return Assemblies.GetOrAdd(key, Assembly.Load(key));
}
}

public static class Assemblies
{
public const string Users = "Armory.Users";
}
}
58 changes: 58 additions & 0 deletions src/Shared/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace Armory.Shared.Helpers
{
public static class ReflectionHelper
{
public static Assembly GetAssemblyByName(string name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}

name = name.ToUpper(CultureInfo.InvariantCulture);
return AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(x =>
x.FullName != null && x.FullName.ToUpper(CultureInfo.InvariantCulture)
.Contains(name, StringComparison.InvariantCulture));
}

public static Type GetType(string name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}

return AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
.FirstOrDefault(type => type.Name.Equals(name, StringComparison.InvariantCulture));
}

public static Type GetType(string assemblyName, string name)
{
if (string.IsNullOrEmpty(assemblyName) && string.IsNullOrEmpty(name))
{
return null;
}

var assembly = GetAssemblyByName(assemblyName);

return GetType(assembly, name);
}

public static Type GetType(Assembly assembly, string name)
{
if (assembly == null)
{
return null;
}

return assembly.GetTypes()
.FirstOrDefault(type => type.Name.Equals(name, StringComparison.InvariantCulture));
}
}
}

0 comments on commit e0d6adf

Please sign in to comment.