Skip to content

Commit

Permalink
Foreach fixed, unnecessary code removed
Browse files Browse the repository at this point in the history
* Fixes foreach to correctly dispose enumerator
* Removed unnecessary code
  • Loading branch information
jvbsl committed Dec 10, 2023
1 parent b0fe182 commit 34a6dc3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
24 changes: 15 additions & 9 deletions NonSucking.Framework.Serialization.Advanced/BaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void EmitForEach(Type enumerableType, Action<BaseGenerator> getEnumerable
if (getCurrent is null)
throw new ArgumentException(
$"Not a valid enumerator(no valid Current get method found): '{enumeratorType.FullName}'");
var dispose = Helper.GetMethodIncludingInterfaces(enumeratorType, "Dispose", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
var dispose = Helper.GetMethodInInterfaces(enumeratorType, "Dispose", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
if (dispose is null)
throw new ArgumentException(
$"Not a valid enumerator(no valid Dispose method found): '{enumeratorType.FullName}'");
Expand Down Expand Up @@ -213,14 +213,20 @@ public void EmitForEach(Type enumerableType, Action<BaseGenerator> getEnumerable
},
(gen, exitLabel) =>
{
// if (!enumeratorVariable.LocalType.IsValueType)
// {
// gen.EmitLoadLocRef(enumeratorVariable);
// gen.IL.Emit(OpCodes.Brfalse, exitLabel);
// }
//
// gen.EmitLoadLocRef(enumeratorVariable);
// gen.IL.Emit(OpCodes.Callvirt, dispose);
bool isValueType = enumeratorVariable.LocalType.IsValueType;
if (!isValueType)
{
gen.EmitLoadLocRef(enumeratorVariable);
gen.Il.Emit(OpCodes.Brfalse, exitLabel);
}
gen.EmitLoadLocRef(enumeratorVariable);
if (isValueType)
{
gen.Il.Emit(OpCodes.Constrained, enumeratorVariable.LocalType);
}
gen.Il.Emit(OpCodes.Callvirt, dispose);
});
}

Expand Down
8 changes: 7 additions & 1 deletion NonSucking.Framework.Serialization.Advanced/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,15 @@ internal static bool MatchIdentifierWithPropName(string identifier, string param
var res = type.GetMethod(name, bindingFlags);
if (res is not null)
return res;

return GetMethodInInterfaces(type, name, bindingFlags);
}

internal static MethodInfo? GetMethodInInterfaces(Type type, string name, BindingFlags bindingFlags)
{
foreach (var i in type.GetInterfaces())
{
res = i.GetMethod(name, bindingFlags);
var res = i.GetMethod(name, bindingFlags);
if (res is not null)
return res;
}
Expand Down
4 changes: 0 additions & 4 deletions NonSucking.Framework.Serialization.Advanced/MethodResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ public static implicit operator AssemblyNameKey(AssemblyName name)
static MethodResolver()
{
// Default resolve extension methods
Stopwatch st = new();
st.Start();
AnalyzeExtensionMethodsRecurse(Assembly.GetCallingAssembly());
AnalyzeExtensionMethodsRecurse(Assembly.GetEntryAssembly());
st.Stop();
Console.WriteLine($"Took {st.ElapsedMilliseconds}ms to load");
}
internal static IEnumerable<MethodInfo> GetRegisteredExtensionMethods(string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static int GetInheritanceDepth(Type? type, int lastAmount = -1)
if (type.GetInterfaces().Any(x => x == typeof(IEnumerable)))
return lastAmount;
type = type.BaseType;
lastAmount = ++lastAmount;
++lastAmount;
}

return lastAmount;
Expand Down

0 comments on commit 34a6dc3

Please sign in to comment.