Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AspectCore.Utils;
using AspectCore.Extensions.Reflection;
using AspectCore.DynamicProxy.ProxyBuilder.Nodes;
using AspectCore.Extensions;

namespace AspectCore.DynamicProxy.ProxyBuilder.Builders
{
Expand Down Expand Up @@ -123,33 +124,75 @@ private List<ConstructorNode> BuildConstructors()
return result;
}

private MethodNode CreateClassProxyMethodNode(MethodInfo method, MethodInfo implMethod, MethodInfo overridesMethod, List<MethodConstantNode> methodConstants)
{
var body = MethodBodyFactory.DecideBody(method, implMethod, _aspectValidator, _serviceType);

var attributes = MethodBuilderConstants.OverrideMethodAttributes;
if (method.Attributes.HasFlag(MethodAttributes.Public))
attributes |= MethodAttributes.Public;
if (method.Attributes.HasFlag(MethodAttributes.Family))
attributes |= MethodAttributes.Family;
if (method.Attributes.HasFlag(MethodAttributes.FamORAssem))
attributes |= MethodAttributes.FamORAssem;

var node = InterfaceImplBuilder.BuildProxyMethod(
serviceMethod: method,
implMethod: implMethod,
name: method.Name,
attributes: attributes,
body: body,
overridesMethod: overridesMethod,
methodConstants: methodConstants);

return node;
}

private void BuildClassMethods(List<MethodNode> methods, List<MethodConstantNode> methodConstants)
{
var covariantReturnMethods = _implType.GetCovariantReturnMethods();

foreach (var method in _serviceType.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(x => !x.IsPropertyBinding()))
{
if (!method.IsVisibleAndVirtual() || Ignores.Contains(method.Name))
continue;

var implMethod = InterfaceImplBuilder.ResolveImplementationMethod(method, _implType);
var body = MethodBodyFactory.DecideBody(method, implMethod, _aspectValidator, _serviceType);
var (covariantReturnMethod, skip) = FindCovariantReturnMethod(method);
if (skip)
continue;

var attributes = MethodBuilderConstants.OverrideMethodAttributes;
if (method.Attributes.HasFlag(MethodAttributes.Public))
attributes |= MethodAttributes.Public;
if (method.Attributes.HasFlag(MethodAttributes.Family))
attributes |= MethodAttributes.Family;
if (method.Attributes.HasFlag(MethodAttributes.FamORAssem))
attributes |= MethodAttributes.FamORAssem;
var (serviceMethod, implMethod) = covariantReturnMethod is null
? (method, InterfaceImplBuilder.ResolveImplementationMethod(method, _implType))
: (covariantReturnMethod, covariantReturnMethod);

var node = InterfaceImplBuilder.BuildProxyMethod(
method, implMethod, method.Name, attributes, body, null, methodConstants);
var node = CreateClassProxyMethodNode(serviceMethod, implMethod, null, methodConstants);
methods.Add(node);
}

(MethodInfo, bool Skip) FindCovariantReturnMethod(MethodInfo method)
{
var covariantReturn = covariantReturnMethods.FirstOrDefault(m => m.OverriddenMethod.IsSameBaseDefinition(method));
var overridden = covariantReturn.OverriddenMethod;
if (overridden == null)
return (null, false);

// If the current method is the base definition of a covariant-return override chain,
// the actual covariant-return method will not appear in _serviceType's method list.
// In that case, use CovariantReturnMethod instead.
//
// Otherwise, the covariant-return method will be visited in a later iteration,
// so skip the current method to avoid duplicate processing.
return overridden.GetBaseDefinition() == method
? (covariantReturn.CovariantReturnMethod, false)
: (null, true);
}
}

private void BuildClassProperties(List<PropertyNode> properties, List<MethodNode> methods, List<MethodConstantNode> methodConstants)
{
var covariantReturnMethods = _implType.GetCovariantReturnMethods();

foreach (var property in _serviceType.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!property.IsVisibleAndVirtual())
Expand All @@ -160,38 +203,29 @@ private void BuildClassProperties(List<PropertyNode> properties, List<MethodNode

if (property.CanRead)
{
var (covariantReturnGetter, skip) = FindCovariantReturnPropertyGetter(property);
if (skip)
continue;

var method = property.GetMethod;
var implMethod = InterfaceImplBuilder.ResolveImplementationMethod(method, _implType);
var body = MethodBodyFactory.DecideBody(method, implMethod, _aspectValidator, _serviceType);
var (serviceMethod, implMethod) = covariantReturnGetter is null
? (method, InterfaceImplBuilder.ResolveImplementationMethod(method, _implType))
: (covariantReturnGetter, covariantReturnGetter);

var attributes = MethodBuilderConstants.OverrideMethodAttributes;
if (method.Attributes.HasFlag(MethodAttributes.Public))
attributes |= MethodAttributes.Public;
if (method.Attributes.HasFlag(MethodAttributes.Family))
attributes |= MethodAttributes.Family;
if (method.Attributes.HasFlag(MethodAttributes.FamORAssem))
attributes |= MethodAttributes.FamORAssem;
// when property.GetMethod is a covariant return type method, we need to define an override for it.
// otherwise, the typeBuilder.CreateTypeInfo() will run forever.
var overrides = property.GetMethod.IsInCovariantReturnChain()
? property.GetMethod
: null;

getMethod = InterfaceImplBuilder.BuildProxyMethod(
method, implMethod, method.Name, attributes, body, null, methodConstants);
getMethod = CreateClassProxyMethodNode(serviceMethod, implMethod, overrides, methodConstants);
}

if (property.CanWrite)
{
var method = property.SetMethod;
var implMethod = InterfaceImplBuilder.ResolveImplementationMethod(method, _implType);
var body = MethodBodyFactory.DecideBody(method, implMethod, _aspectValidator, _serviceType);

var attributes = MethodBuilderConstants.OverrideMethodAttributes;
if (method.Attributes.HasFlag(MethodAttributes.Public))
attributes |= MethodAttributes.Public;
if (method.Attributes.HasFlag(MethodAttributes.Family))
attributes |= MethodAttributes.Family;
if (method.Attributes.HasFlag(MethodAttributes.FamORAssem))
attributes |= MethodAttributes.FamORAssem;

setMethod = InterfaceImplBuilder.BuildProxyMethod(
method, implMethod, method.Name, attributes, body, null, methodConstants);
setMethod = CreateClassProxyMethodNode(method, implMethod, null, methodConstants);
}

var attrs = new List<AttributeNode>
Expand All @@ -202,6 +236,42 @@ private void BuildClassProperties(List<PropertyNode> properties, List<MethodNode

properties.Add(new PropertyNode(property.Name, property.PropertyType, property.Attributes, attrs, getMethod, setMethod));
}

(MethodInfo, bool Skip) FindCovariantReturnPropertyGetter(PropertyInfo property)
{
var covariantReturn = covariantReturnMethods.FirstOrDefault(m => IsOverriddenByCovariantReturnProperty(property, m));
var overridden = covariantReturn.OverriddenMethod;
if (overridden == null)
return (null, false);

// If the property's getter is the base definition of a covariant-return override chain,
// the actual covariant-return getter will not appear in _serviceType's property list.
// In that case, use CovariantReturnMethod instead.
//
// Otherwise, the covariant-return property will be visited in a later iteration,
// so skip the current property to avoid duplicate processing.
return overridden.GetBaseDefinition() == property.GetMethod
? (covariantReturn.CovariantReturnMethod, false)
: (null, true);
}

bool IsOverriddenByCovariantReturnProperty(PropertyInfo property, CovariantReturnMethodInfo info)
{
// this case occurs when the property is not overridden in the serviceType.
var get = property.GetMethod;
if (info.OverriddenMethod.IsSameBaseDefinition(get))
return true;

// this case occurs when the property is overridden in the serviceType.
// in this case, the property type is super class of (and not the same as) the getter's type.
var covariantReturn = info.CovariantReturnMethod;
if (covariantReturn.IsSameBaseDefinition(get)
&& covariantReturn.ReturnType != property.PropertyType
&& property.PropertyType.IsAssignableFrom(covariantReturn.ReturnType))
return true;

return false;
}
}

private void BuildAdditionalInterfaceMembers(List<MethodNode> methods, List<PropertyNode> properties, List<MethodConstantNode> methodConstants)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AspectCore.Utils;
using AspectCore.Extensions.Reflection;
using AspectCore.DynamicProxy.ProxyBuilder.Nodes;
using AspectCore.Extensions;

namespace AspectCore.DynamicProxy.ProxyBuilder.Builders
{
Expand Down Expand Up @@ -239,11 +240,13 @@ internal static void BuildInterfaceProxyMembers(
List<MethodConstantNode> methodConstants)
{
var resolvedImplType = implType ?? interfaceType;
var covariantReturnMethods = resolvedImplType.GetCovariantReturnMethods();

// Primary interface methods
foreach (var method in interfaceType.GetTypeInfo().DeclaredMethods.Where(x => !x.IsPropertyBinding()))
{
var implMethod = ResolveImplementationMethod(method, resolvedImplType);
var covariantReturnMethod = FindCovariantReturnMethod(method);
var implMethod = covariantReturnMethod ?? ResolveImplementationMethod(method, resolvedImplType);
var body = MethodBodyFactory.DecideBody(method, implMethod, aspectValidator, interfaceType);
var node = BuildProxyMethod(method, implMethod, method.Name,
MethodBuilderConstants.InterfaceMethodAttributes, body, method, methodConstants);
Expand All @@ -255,7 +258,8 @@ internal static void BuildInterfaceProxyMembers(
{
foreach (var method in iface.GetTypeInfo().DeclaredMethods.Where(x => !x.IsPropertyBinding()))
{
var implMethod = ResolveImplementationMethod(method, resolvedImplType);
var covariantReturnMethod = FindCovariantReturnMethod(method);
var implMethod = covariantReturnMethod ?? ResolveImplementationMethod(method, resolvedImplType);
var body = MethodBodyFactory.DecideBody(method, implMethod, aspectValidator, interfaceType);
var node = BuildProxyMethod(method, implMethod, method.GetName(),
MethodBuilderConstants.ExplicitMethodAttributes, body, method, methodConstants);
Expand All @@ -266,19 +270,35 @@ internal static void BuildInterfaceProxyMembers(
// Primary interface properties
foreach (var property in interfaceType.GetTypeInfo().DeclaredProperties)
{
var covariantReturnGetter = FindCovariantReturnGetter(property);
properties.Add(BuildProxyProperty(property, property.Name, resolvedImplType, aspectValidator,
interfaceType, MethodBuilderConstants.InterfaceMethodAttributes, methods, methodConstants));
interfaceType, MethodBuilderConstants.InterfaceMethodAttributes, methods, methodConstants, covariantReturnGetter));
}

// Additional interface properties (explicit)
foreach (var iface in additionalInterfaces)
{
foreach (var property in iface.GetTypeInfo().DeclaredProperties)
{
var covariantReturnGetter = FindCovariantReturnGetter(property);
properties.Add(BuildProxyProperty(property, property.GetDisplayName(), resolvedImplType, aspectValidator,
interfaceType, MethodBuilderConstants.ExplicitMethodAttributes, methods, methodConstants));
interfaceType, MethodBuilderConstants.ExplicitMethodAttributes, methods, methodConstants, covariantReturnGetter));
}
}

MethodInfo FindCovariantReturnMethod(MethodInfo interfaceMethod)
{
return covariantReturnMethods
.FirstOrDefault(m => m.InterfaceDeclarations.Contains(interfaceMethod))
.CovariantReturnMethod;
}

MethodInfo FindCovariantReturnGetter(PropertyInfo property)
{
return property.CanRead
? covariantReturnMethods.FirstOrDefault(m => m.InterfaceDeclarations.Contains(property.GetMethod)).CovariantReturnMethod
: null;
}
}

private static PropertyNode BuildProxyProperty(
Expand All @@ -289,15 +309,16 @@ private static PropertyNode BuildProxyProperty(
Type serviceType,
MethodAttributes methodAttrs,
List<MethodNode> methods,
List<MethodConstantNode> methodConstants)
List<MethodConstantNode> methodConstants,
MethodInfo covariantReturnGetter)
{
MethodNode getMethod = null;
MethodNode setMethod = null;

if (property.CanRead)
{
var method = property.GetMethod;
var implMethod = ResolveImplementationMethod(method, implType);
var implMethod = covariantReturnGetter ?? ResolveImplementationMethod(method, implType);
var body = MethodBodyFactory.DecideBody(method, implMethod, aspectValidator, serviceType);
var overrides = methodAttrs == MethodBuilderConstants.ExplicitMethodAttributes ? method : method;
getMethod = BuildProxyMethod(method, implMethod, methodAttrs == MethodBuilderConstants.ExplicitMethodAttributes ? method.GetName() : method.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ private Type CreateClassProxyInternal(string name, Type serviceType, Type implTy

private class ProxyNameUtils
{
private readonly Dictionary<string, ProxyNameIndex> _indexs = new Dictionary<string, ProxyNameIndex>();
private readonly Dictionary<string, ProxyNameIndex> _indexes = new Dictionary<string, ProxyNameIndex>();
private readonly Dictionary<Tuple<Type, Type>, string> _indexMaps = new Dictionary<Tuple<Type, Type>, string>();

private string GetProxyTypeIndex(string className, Type serviceType, Type implementationType)
{
ProxyNameIndex nameIndex;
if (!_indexs.TryGetValue(className, out nameIndex))
if (!_indexes.TryGetValue(className, out nameIndex))
{
nameIndex = new ProxyNameIndex();
_indexs[className] = nameIndex;
_indexes[className] = nameIndex;
}
var key = Tuple.Create(serviceType, implementationType);
string index;
Expand Down
15 changes: 15 additions & 0 deletions src/AspectCore.Core/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ReSharper disable once CheckNamespace
namespace System.Collections.Generic
{
internal static class CollectionExtensions
{
#if NETSTANDARD2_0
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
return dictionary.TryGetValue(key, out var obj)
? obj
: defaultValue;
}
#endif
}
}
22 changes: 22 additions & 0 deletions src/AspectCore.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

// ReSharper disable once CheckNamespace
namespace System.Linq
{
internal static class EnumerableExtensions
{
#if NETSTANDARD2_0 || NETSTANDARD2_1
public static IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second)
{
return first.Zip(second, (f, s) => (f, s));
}
#endif

#if NETSTANDARD2_0
public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer = null)
{
return new HashSet<TSource>(source, comparer);
}
#endif
}
}
Loading
Loading