-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyExtensions.cs
56 lines (52 loc) · 1.78 KB
/
AssemblyExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using Platform.Exceptions;
using Platform.Collections.Lists;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Platform.Reflection
{
/// <summary>
/// <para>
/// Represents the assembly extensions.
/// </para>
/// <para></para>
/// </summary>
public static class AssemblyExtensions
{
private static readonly ConcurrentDictionary<Assembly, Type[]> _loadableTypesCache = new ConcurrentDictionary<Assembly, Type[]>();
/// <remarks>
/// Source: http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx/
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] GetLoadableTypes(this Assembly assembly)
{
Ensure.Always.ArgumentNotNull(assembly, nameof(assembly));
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.ToArray(t => t != null);
}
}
/// <summary>
/// <para>
/// Gets the cached loadable types using the specified assembly.
/// </para>
/// <para></para>
/// </summary>
/// <param name="assembly">
/// <para>The assembly.</para>
/// <para></para>
/// </param>
/// <returns>
/// <para>The type array</para>
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] GetCachedLoadableTypes(this Assembly assembly) => _loadableTypesCache.GetOrAdd(assembly, GetLoadableTypes);
}
}