-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Assembly and Reflection helpers
- Loading branch information
1 parent
0badda1
commit e0d6adf
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |