Skip to content

Commit 3e4262a

Browse files
authored
Add more try-catch logic to DefaultAssemblyHelper and AbstractDynamicLinqCustomTypeProvider (#684)
* Add more try-catch to DefaultAssemblyHelper * 1-preview-01 * . * . * v
1 parent 0f44d99 commit 3e4262a

File tree

4 files changed

+75
-44
lines changed

4 files changed

+75
-44
lines changed

src/System.Linq.Dynamic.Core/CustomTypeProviders/AbstractDynamicLinqCustomTypeProvider.cs

+17-6
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumer
126126
Check.NotNull(assemblies);
127127

128128
#if !NET5_0_OR_GREATER
129-
assemblies = assemblies.Where(a => !a.GlobalAssemblyCache); // Skip System DLL's
129+
assemblies = assemblies.Where(a => !a.GlobalAssemblyCache).ToArray(); // Skip System DLL's
130130
#endif
131-
132131
foreach (var assembly in assemblies)
133132
{
134133
var definedTypes = Type.EmptyTypes;
@@ -146,11 +145,23 @@ protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumer
146145
// Ignore all other exceptions
147146
}
148147

149-
var filteredAndDistinct = definedTypes
150-
.Where(t => t.IsDefined(typeof(DynamicLinqTypeAttribute), false))
151-
.Distinct();
148+
var filtered = new List<Type>();
149+
foreach (var definedType in definedTypes)
150+
{
151+
try
152+
{
153+
if (definedType.IsDefined(typeof(DynamicLinqTypeAttribute), false))
154+
{
155+
filtered.Add(definedType);
156+
}
157+
}
158+
catch
159+
{
160+
// Ignore
161+
}
162+
}
152163

153-
foreach (var definedType in filteredAndDistinct)
164+
foreach (var definedType in filtered.Distinct().ToArray())
154165
{
155166
yield return definedType;
156167
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
11
using System.IO;
22
using System.Reflection;
3+
using System.Collections.Generic;
34

4-
namespace System.Linq.Dynamic.Core
5+
namespace System.Linq.Dynamic.Core;
6+
7+
internal class DefaultAssemblyHelper : IAssemblyHelper
58
{
6-
internal class DefaultAssemblyHelper : IAssemblyHelper
9+
public Assembly[] GetAssemblies()
710
{
8-
public Assembly[] GetAssemblies()
9-
{
1011
#if WINDOWS_APP || UAP10_0 || NETSTANDARD || WPSL
11-
throw new NotSupportedException();
12+
throw new NotSupportedException();
1213
#elif NET35
13-
14-
return AppDomain.CurrentDomain.GetAssemblies();
14+
return AppDomain.CurrentDomain.GetAssemblies();
1515
#else
16-
// https://stackoverflow.com/a/2384679/255966
17-
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
18-
var loadedPaths = loadedAssemblies.Where(a => !a.IsDynamic).Select(a => a.Location).ToArray();
19-
20-
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
21-
var pathsToLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase));
22-
23-
foreach (var path in pathsToLoad)
16+
// https://stackoverflow.com/a/2384679/255966
17+
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
18+
var loadedPaths = new List<string>();
19+
foreach (var loadedAssembly in loadedAssemblies)
20+
{
21+
try
2422
{
25-
try
23+
if (!loadedAssembly.IsDynamic)
2624
{
27-
loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)));
28-
}
29-
catch
30-
{
31-
// Ignore
25+
loadedPaths.Add(loadedAssembly.Location);
3226
}
3327
}
28+
catch
29+
{
30+
// Ignore
31+
}
32+
}
3433

35-
return loadedAssemblies.ToArray();
36-
#endif
34+
string[] referencedPaths;
35+
try
36+
{
37+
referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
38+
}
39+
catch
40+
{
41+
referencedPaths = new string[0];
42+
}
43+
44+
var pathsToLoad = referencedPaths.Where(referencedPath => !loadedPaths.Contains(referencedPath, StringComparer.InvariantCultureIgnoreCase));
45+
foreach (var path in pathsToLoad)
46+
{
47+
try
48+
{
49+
loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)));
50+
}
51+
catch
52+
{
53+
// Ignore
54+
}
3755
}
56+
57+
return loadedAssemblies.ToArray();
58+
#endif
3859
}
3960
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using System.Reflection;
22

3-
namespace System.Linq.Dynamic.Core
3+
namespace System.Linq.Dynamic.Core;
4+
5+
/// <summary>
6+
/// IAssemblyHelper interface which is used to retrieve assemblies that have been loaded into the execution context of this application domain.
7+
/// </summary>
8+
public interface IAssemblyHelper
49
{
510
/// <summary>
6-
/// IAssemblyHelper interface which is used to retrieve assemblies that have been loaded into the execution context of this application domain.
11+
/// Gets the assemblies that have been loaded into the execution context of this application domain.
712
/// </summary>
8-
public interface IAssemblyHelper
9-
{
10-
/// <summary>
11-
/// Gets the assemblies that have been loaded into the execution context of this application domain.
12-
/// </summary>
13-
///
14-
/// <returns>
15-
/// An array of assemblies in this application domain.
16-
/// </returns>
17-
Assembly[] GetAssemblies();
18-
}
13+
///
14+
/// <returns>
15+
/// An array of assemblies in this application domain.
16+
/// </returns>
17+
Assembly[] GetAssemblies();
1918
}

version.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<PatchVersion>0</PatchVersion>
3+
<PatchVersion>1</PatchVersion>
44
</PropertyGroup>
5-
</Project>
5+
</Project>

0 commit comments

Comments
 (0)