Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
76 changes: 41 additions & 35 deletions src/Polyfill/Polyfill_Type.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Polyfills;

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down Expand Up @@ -58,49 +59,54 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
}
}

var methods = target.GetMethods(bindingAttr);
if (genericParameterCount == 0)
var comparison = StringComparison.Ordinal;
if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
{
foreach (var method in methods)
{
if (method.IsGenericMethod)
{
continue;
}

if (IsMatch(method))
{
return method;
}
}
comparison = StringComparison.OrdinalIgnoreCase;
}
else

// Narrow to the candidates by name, generic arity and parameter count,
// then let the binder pick the best match for the argument types.
var candidates = new List<MethodBase>();
foreach (var method in target.GetMethods(bindingAttr))
{
foreach (var method in methods)
if (!string.Equals(name, method.Name, comparison))
{
continue;
}

var genericArgumentCount = 0;
if (method.IsGenericMethod)
{
genericArgumentCount = method.GetGenericArguments().Length;
}

if (genericParameterCount != genericArgumentCount)
{
if (!method.IsGenericMethod)
{
continue;
}

var genericArguments = method.GetGenericArguments();
if (genericParameterCount != genericArguments.Length)
{
continue;
}

if (IsMatch(method))
{
return method;
}
continue;
}

if (method.GetParameters().Length != types.Length)
{
continue;
}

candidates.Add(method);
}

return null;
if (candidates.Count == 0)
{
return null;
}

if (types.Length == 0 &&
candidates.Count == 1)
{
return (MethodInfo) candidates[0];
}

bool IsMatch(MethodInfo method) =>
name == method.Name &&
method.GetParameters().Select(_ => _.ParameterType).SequenceEqual(types);
binder ??= Type.DefaultBinder;
return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as MethodInfo;
}
#endif

Expand Down
1 change: 1 addition & 0 deletions src/Split/net10.0/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable
namespace Polyfills;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions src/Split/net11.0/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable
namespace Polyfills;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down
65 changes: 33 additions & 32 deletions src/Split/net461/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable
namespace Polyfills;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down Expand Up @@ -47,44 +48,44 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
throw new ArgumentNullException(nameof(types));
}
}
var methods = target.GetMethods(bindingAttr);
if (genericParameterCount == 0)
var comparison = StringComparison.Ordinal;
if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
{
foreach (var method in methods)
{
if (method.IsGenericMethod)
{
continue;
}
if (IsMatch(method))
{
return method;
}
}
comparison = StringComparison.OrdinalIgnoreCase;
}
else
var candidates = new List<MethodBase>();
foreach (var method in target.GetMethods(bindingAttr))
{
foreach (var method in methods)
if (!string.Equals(name, method.Name, comparison))
{
continue;
}
var genericArgumentCount = 0;
if (method.IsGenericMethod)
{
genericArgumentCount = method.GetGenericArguments().Length;
}
if (genericParameterCount != genericArgumentCount)
{
if (!method.IsGenericMethod)
{
continue;
}
var genericArguments = method.GetGenericArguments();
if (genericParameterCount != genericArguments.Length)
{
continue;
}
if (IsMatch(method))
{
return method;
}
continue;
}
if (method.GetParameters().Length != types.Length)
{
continue;
}
candidates.Add(method);
}
if (candidates.Count == 0)
{
return null;
}
if (types.Length == 0 &&
candidates.Count == 1)
{
return (MethodInfo) candidates[0];
}
return null;
bool IsMatch(MethodInfo method) =>
name == method.Name &&
method.GetParameters().Select(_ => _.ParameterType).SequenceEqual(types);
binder ??= Type.DefaultBinder;
return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as MethodInfo;
}
/// <summary>
/// Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints.
Expand Down
65 changes: 33 additions & 32 deletions src/Split/net462/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable
namespace Polyfills;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down Expand Up @@ -47,44 +48,44 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
throw new ArgumentNullException(nameof(types));
}
}
var methods = target.GetMethods(bindingAttr);
if (genericParameterCount == 0)
var comparison = StringComparison.Ordinal;
if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
{
foreach (var method in methods)
{
if (method.IsGenericMethod)
{
continue;
}
if (IsMatch(method))
{
return method;
}
}
comparison = StringComparison.OrdinalIgnoreCase;
}
else
var candidates = new List<MethodBase>();
foreach (var method in target.GetMethods(bindingAttr))
{
foreach (var method in methods)
if (!string.Equals(name, method.Name, comparison))
{
continue;
}
var genericArgumentCount = 0;
if (method.IsGenericMethod)
{
genericArgumentCount = method.GetGenericArguments().Length;
}
if (genericParameterCount != genericArgumentCount)
{
if (!method.IsGenericMethod)
{
continue;
}
var genericArguments = method.GetGenericArguments();
if (genericParameterCount != genericArguments.Length)
{
continue;
}
if (IsMatch(method))
{
return method;
}
continue;
}
if (method.GetParameters().Length != types.Length)
{
continue;
}
candidates.Add(method);
}
if (candidates.Count == 0)
{
return null;
}
if (types.Length == 0 &&
candidates.Count == 1)
{
return (MethodInfo) candidates[0];
}
return null;
bool IsMatch(MethodInfo method) =>
name == method.Name &&
method.GetParameters().Select(_ => _.ParameterType).SequenceEqual(types);
binder ??= Type.DefaultBinder;
return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as MethodInfo;
}
/// <summary>
/// Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints.
Expand Down
65 changes: 33 additions & 32 deletions src/Split/net47/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable
namespace Polyfills;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Linq;
Expand Down Expand Up @@ -47,44 +48,44 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
throw new ArgumentNullException(nameof(types));
}
}
var methods = target.GetMethods(bindingAttr);
if (genericParameterCount == 0)
var comparison = StringComparison.Ordinal;
if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
{
foreach (var method in methods)
{
if (method.IsGenericMethod)
{
continue;
}
if (IsMatch(method))
{
return method;
}
}
comparison = StringComparison.OrdinalIgnoreCase;
}
else
var candidates = new List<MethodBase>();
foreach (var method in target.GetMethods(bindingAttr))
{
foreach (var method in methods)
if (!string.Equals(name, method.Name, comparison))
{
continue;
}
var genericArgumentCount = 0;
if (method.IsGenericMethod)
{
genericArgumentCount = method.GetGenericArguments().Length;
}
if (genericParameterCount != genericArgumentCount)
{
if (!method.IsGenericMethod)
{
continue;
}
var genericArguments = method.GetGenericArguments();
if (genericParameterCount != genericArguments.Length)
{
continue;
}
if (IsMatch(method))
{
return method;
}
continue;
}
if (method.GetParameters().Length != types.Length)
{
continue;
}
candidates.Add(method);
}
if (candidates.Count == 0)
{
return null;
}
if (types.Length == 0 &&
candidates.Count == 1)
{
return (MethodInfo) candidates[0];
}
return null;
bool IsMatch(MethodInfo method) =>
name == method.Name &&
method.GetParameters().Select(_ => _.ParameterType).SequenceEqual(types);
binder ??= Type.DefaultBinder;
return binder.SelectMethod(bindingAttr, candidates.ToArray(), types, modifiers) as MethodInfo;
}
/// <summary>
/// Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints.
Expand Down
Loading
Loading