Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logic for AbstractDynamicLinqCustomTypeProvider.GetAssemblyTypesWithDynamicLinqTypeAttribute #703

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions System.Linq.Dynamic.Core.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IL/@EntryIndexedValue">IL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UTC/@EntryIndexedValue">UTC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WASM/@EntryIndexedValue">WASM</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DLL_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=renamer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xunit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ protected IEnumerable<Type> FindTypesMarkedWithDynamicLinqTypeAttribute(IEnumera
/// Gets the assembly types annotated with <see cref="DynamicLinqTypeAttribute"/> in an Exception friendly way.
/// </summary>
/// <param name="assemblies">The assemblies to process.</param>
/// <returns><see cref="IEnumerable{Type}" /></returns>
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
/// <returns>Array of <see cref="Type" /></returns>
protected Type[] GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
{
Check.NotNull(assemblies);

var dynamicLinqTypes = new List<Type>();

foreach (var assembly in assemblies)
{
var definedTypes = Type.EmptyTypes;
Type[] definedTypes;

try
{
definedTypes = assembly.ExportedTypes.ToArray();
definedTypes = assembly.GetExportedTypes();
}
catch (ReflectionTypeLoadException reflectionTypeLoadException)
{
Expand All @@ -103,38 +105,49 @@ protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumer
catch
{
// Ignore all other exceptions
definedTypes = Type.EmptyTypes;
}

var filteredAndDistinct = definedTypes
.Where(t => t.GetTypeInfo().IsDefined(typeof(DynamicLinqTypeAttribute), false))
.Distinct();

foreach (var definedType in filteredAndDistinct)
foreach (var definedType in definedTypes)
{
yield return definedType;
try
{
if (definedType.GetTypeInfo().IsDefined(typeof(DynamicLinqTypeAttribute), false))
{
dynamicLinqTypes.Add(definedType);
}
}
catch
{
// Ignore
}
}
}

return dynamicLinqTypes.Distinct().ToArray();
}
#else
/// <summary>
/// Gets the assembly types annotated with <see cref="DynamicLinqTypeAttribute"/> in an Exception friendly way.
/// </summary>
/// <param name="assemblies">The assemblies to process.</param>
/// <returns><see cref="IEnumerable{Type}" /></returns>
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
/// <returns>Array of <see cref="Type" /></returns>
protected Type[] GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
{
Check.NotNull(assemblies);

var dynamicLinqTypes = new List<Type>();

#if !NET5_0_OR_GREATER
assemblies = assemblies.Where(a => !a.GlobalAssemblyCache).ToArray(); // Skip System DLL's
#endif
foreach (var assembly in assemblies)
{
var definedTypes = Type.EmptyTypes;
Type[] definedTypes;

try
{
definedTypes = assembly.GetExportedTypes().ToArray();
definedTypes = assembly.GetTypes();
}
catch (ReflectionTypeLoadException reflectionTypeLoadException)
{
Expand All @@ -143,29 +156,26 @@ protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumer
catch
{
// Ignore all other exceptions
definedTypes = Type.EmptyTypes;
}

var filtered = new List<Type>();
foreach (var definedType in definedTypes)
{
try
{
if (definedType.IsDefined(typeof(DynamicLinqTypeAttribute), false))
{
filtered.Add(definedType);
dynamicLinqTypes.Add(definedType);
}
}
catch
{
// Ignore
}
}

foreach (var definedType in filtered.Distinct().ToArray())
{
yield return definedType;
}
}

return dynamicLinqTypes.Distinct().ToArray();
}
#endif
}