Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f05a7dc
Force explicit choice for `Reference.owner`
stakx Dec 14, 2025
295ccc5
Force explicit choice for `TypeReference.owner`
stakx Dec 14, 2025
3c90ed9
Force explicit choice for `MethodInvocationExpression.owner`
stakx Dec 14, 2025
1848217
Remove superfluous `MethodInvocationExpression` ctor
stakx Dec 14, 2025
cc78232
Downgrade `MethodInvocationExpression.owner` to an expression
stakx Dec 14, 2025
2d456b4
Downgrade contributors' & generators' `getTarget` to an expression
stakx Dec 14, 2025
54343bc
Downgrade `AsTypeReference` to an expression & rename it
stakx Dec 14, 2025
ce09a89
`Reference` does not need a recursive owner chain
stakx Dec 14, 2025
2e31b94
Downgrade `SelfReference` to an expression & rename it
stakx Dec 14, 2025
89795bf
Merge `TypeReference` into `Reference`
stakx Dec 14, 2025
c91cc9d
Merge `ArgumentsUtil.EmitLoadOwnerAndReference` into `Reference`
stakx Dec 14, 2025
b1ec8cd
Move logic from `AssignStatement` into `Reference.StoreReference`
stakx Dec 14, 2025
8579344
Move loading of `Reference.owner` into `LoadReference`
stakx Dec 14, 2025
db7e83b
Not all `Reference`s have an owner
stakx Dec 14, 2025
a0dc312
Decouple `AssignArgumentStatement` and `DefaultValueExpression`
stakx Dec 14, 2025
2dcb49b
Split up `AssignArrayStatement`
stakx Dec 14, 2025
4fa838d
Downgrade `ByRefReference` to an expression & rename it
stakx Dec 14, 2025
6686011
Merge identical code paths for expressions and references
stakx Dec 14, 2025
cf0b632
`LoadRefArrayElementExpression` has been superseded
stakx Dec 15, 2025
c3d65f8
Rename `ReferencesToObjectArrayExpression`
stakx Dec 15, 2025
ba565d9
Finetune a few `[DebuggerDisplay]`s
stakx Dec 15, 2025
56bee54
Rename all `Reference`s to `Location`s
stakx Dec 15, 2025
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 @@ -33,7 +33,7 @@ internal class ClassProxySerializableContributor : SerializableContributor
{
private bool delegateToBaseGetObjectData;
private ConstructorInfo serializationConstructor;
private readonly IList<FieldReference> serializedFields = new List<FieldReference>();
private readonly IList<FieldLocation> serializedFields = new List<FieldLocation>();

public ClassProxySerializableContributor(Type targetType, Type[] interfaces, string typeId)
: base(targetType, interfaces, typeId)
Expand Down Expand Up @@ -82,15 +82,15 @@ public override void Generate(ClassEmitter @class)
Constructor(@class);
}

protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,
FieldReference field)
protected override void AddAddValueInvocation(ArgumentLocation serializationInfo, MethodEmitter getObjectData,
FieldLocation field)
{
serializedFields.Add(field);
base.AddAddValueInvocation(serializationInfo, getObjectData, field);
}

protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo,
ArgumentReference streamingContext, ClassEmitter emitter)
protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo,
ArgumentLocation streamingContext, ClassEmitter emitter)
{
codeBuilder.AddStatement(
new MethodInvocationExpression(
Expand All @@ -108,7 +108,7 @@ protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, Argument
EmitCallToBaseGetObjectData(codeBuilder, serializationInfo, streamingContext);
}

private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo)
private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo)
{
var members = codeBuilder.DeclareLocal(typeof(MemberInfo[]));
var data = codeBuilder.DeclareLocal(typeof(object[]));
Expand All @@ -129,7 +129,7 @@ private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference
var getObjectData = new MethodInvocationExpression(
null,
FormatterServicesMethods.GetObjectData,
SelfReference.Self,
ThisExpression.Instance,
members);
codeBuilder.AddStatement(new AssignStatement(data, getObjectData));

Expand All @@ -141,14 +141,15 @@ private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference
codeBuilder.AddStatement(addValue);
}

private void EmitCallToBaseGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo,
ArgumentReference streamingContext)
private void EmitCallToBaseGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo,
ArgumentLocation streamingContext)
{
var baseGetObjectData = targetType.GetMethod("GetObjectData",
new[] { typeof(SerializationInfo), typeof(StreamingContext) });

codeBuilder.AddStatement(
new MethodInvocationExpression(
ThisExpression.Instance,
baseGetObjectData,
serializationInfo,
streamingContext));
Expand All @@ -165,8 +166,8 @@ private void Constructor(ClassEmitter emitter)

private void GenerateSerializationConstructor(ClassEmitter emitter)
{
var serializationInfo = new ArgumentReference(typeof(SerializationInfo));
var streamingContext = new ArgumentReference(typeof(StreamingContext));
var serializationInfo = new ArgumentLocation(typeof(SerializationInfo));
var streamingContext = new ArgumentLocation(typeof(StreamingContext));

var ctor = emitter.CreateConstructor(serializationInfo, streamingContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm

var invocation = GetInvocationType(method, @class);

GetTargetExpressionDelegate getTargetTypeExpression = (c, m) => new TypeTokenExpression(targetType);
GetTargetExpressionDelegate getTargetType = (c, m) => new TypeTokenExpression(targetType);

return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
invocation,
getTargetTypeExpression,
getTargetTypeExpression,
getTargetType,
getTargetType,
overrideMethod,
null);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ private MethodBuilder CreateCallbackMethod(ClassEmitter emitter, MethodInfo meth

callBackMethod.CodeBuilder.AddStatement(
new ReturnStatement(
new MethodInvocationExpression(SelfReference.Self,
new MethodInvocationExpression(ThisExpression.Instance,
targetMethod,
callBackMethod.Arguments)));

Expand Down Expand Up @@ -158,7 +158,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod
}
return new InvocationWithGenericDelegateContributor(@delegate,
method,
new FieldReference(InvocationMethods.ProxyObject));
new FieldLocation(InvocationMethods.ProxyObject));
}

private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod
}
return new InvocationWithGenericDelegateContributor(@delegate,
method,
new FieldReference(InvocationMethods.CompositionInvocationTarget));
new FieldLocation(InvocationMethods.CompositionInvocationTarget));
}

private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
Expand Down
2 changes: 0 additions & 2 deletions src/Castle.Core/DynamicProxy/Contributors/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ internal delegate MethodEmitter OverrideMethodDelegate(
string name, MethodAttributes attributes, MethodInfo methodToOverride);

internal delegate IExpression GetTargetExpressionDelegate(ClassEmitter @class, MethodInfo method);

internal delegate Reference GetTargetReferenceDelegate(ClassEmitter @class, MethodInfo method);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace Castle.DynamicProxy.Contributors

internal interface IInvocationCreationContributor
{
ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation);
ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation);

MethodInfo GetCallbackMethod();

MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
Reference targetField, MethodEmitter invokeMethodOnTarget);
Location targetField, MethodEmitter invokeMethodOnTarget);

IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public InterfaceProxySerializableContributor(Type targetType, string proxyGenera
{
}

protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo,
ArgumentReference streamingContext, ClassEmitter emitter)
protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo,
ArgumentLocation streamingContext, ClassEmitter emitter)
{
var targetField = emitter.GetField("__target");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ namespace Castle.DynamicProxy.Contributors

internal class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWithoutTargetContributor
{
private readonly GetTargetReferenceDelegate getTargetReference;

public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget,
GetTargetReferenceDelegate getTargetReference)
public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget)
: base(namingScope, getTarget)
{
this.getTargetReference = getTargetReference;
canChangeTarget = true;
}

Expand All @@ -34,7 +30,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm
{
if (!method.Proxyable)
{
return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTargetReference);
return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTarget);
}

return base.GetMethodGenerator(method, @class, overrideMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ namespace Castle.DynamicProxy.Contributors

internal class InterfaceProxyWithoutTargetContributor : CompositeTypeContributor
{
private readonly GetTargetExpressionDelegate getTargetExpression;
protected readonly GetTargetExpressionDelegate getTarget;
protected bool canChangeTarget = false;

public InterfaceProxyWithoutTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget)
: base(namingScope)
{
getTargetExpression = getTarget;
this.getTarget = getTarget;
}

protected override IEnumerable<MembersCollector> GetCollectors()
Expand All @@ -56,7 +56,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm
return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
invocation,
getTargetExpression,
getTarget,
overrideMethod,
null);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ private MethodInfo CreateCallbackMethod(ClassEmitter emitter, MethodInfo methodI

callBackMethod.CodeBuilder.AddStatement(
new ReturnStatement(
new MethodInvocationExpression(SelfReference.Self, targetMethod, callBackMethod.Arguments)));
new MethodInvocationExpression(ThisExpression.Instance, targetMethod, callBackMethod.Arguments)));

return callBackMethod.MethodBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public InvocationWithDelegateContributor(Type delegateType, Type targetType, Met
this.namingScope = namingScope;
}

public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
public ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation)
{
var arguments = GetArguments(baseCtorArguments);
var constructor = invocation.CreateConstructor(arguments);
Expand All @@ -56,11 +56,11 @@ public MethodInfo GetCallbackMethod()
}

public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
Reference targetField,
Location targetField,
MethodEmitter invokeMethodOnTarget)
{
var allArgs = GetAllArgs(args, targetField);
var @delegate = (Reference)invocation.GetField("delegate");
var @delegate = (Location)invocation.GetField("delegate");

return new MethodInvocationExpression(@delegate, GetCallbackMethod(), allArgs);
}
Expand All @@ -73,7 +73,7 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments,
return allArguments;
}

private FieldReference BuildDelegateToken(ClassEmitter proxy)
private FieldLocation BuildDelegateToken(ClassEmitter proxy)
{
var callback = proxy.CreateStaticField(namingScope.GetUniqueName("callback_" + method.Method.Name), delegateType);
var createDelegate = new MethodInvocationExpression(
Expand All @@ -88,18 +88,18 @@ private FieldReference BuildDelegateToken(ClassEmitter proxy)
return callback;
}

private IExpression[] GetAllArgs(IExpression[] args, Reference targetField)
private IExpression[] GetAllArgs(IExpression[] args, Location targetField)
{
var allArgs = new IExpression[args.Length + 1];
args.CopyTo(allArgs, 1);
allArgs[0] = new ConvertExpression(targetType, targetField);
return allArgs;
}

private ArgumentReference[] GetArguments(ArgumentReference[] baseCtorArguments)
private ArgumentLocation[] GetArguments(ArgumentLocation[] baseCtorArguments)
{
var arguments = new ArgumentReference[baseCtorArguments.Length + 1];
arguments[0] = new ArgumentReference(delegateType);
var arguments = new ArgumentLocation[baseCtorArguments.Length + 1];
arguments[0] = new ArgumentLocation(delegateType);
baseCtorArguments.CopyTo(arguments, 1);
return arguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ internal class InvocationWithGenericDelegateContributor : IInvocationCreationCon
{
private readonly Type delegateType;
private readonly MetaMethod method;
private readonly Reference targetReference;
private readonly IExpression target;

public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, Reference targetReference)
public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, IExpression target)
{
Debug.Assert(delegateType.IsGenericType, "delegateType.IsGenericType");
this.delegateType = delegateType;
this.method = method;
this.targetReference = targetReference;
this.target = target;
}

public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
public ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation)
{
return invocation.CreateConstructor(baseCtorArguments);
}
Expand All @@ -50,7 +50,7 @@ public MethodInfo GetCallbackMethod()
}

public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
Reference targetField,
Location targetField,
MethodEmitter invokeMethodOnTarget)
{
var @delegate = GetDelegate(invocation, invokeMethodOnTarget);
Expand All @@ -62,25 +62,25 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments,
return arguments;
}

private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
private Location GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
{
var genericTypeParameters = invocation.GenericTypeParams.AsTypeArray();
var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters);
var localReference = invokeMethodOnTarget.CodeBuilder.DeclareLocal(closedDelegateType);
var closedMethodOnTarget = method.MethodOnTarget.MakeGenericMethod(genericTypeParameters);
invokeMethodOnTarget.CodeBuilder.AddStatement(
SetDelegate(localReference, targetReference, closedDelegateType, closedMethodOnTarget));
SetDelegate(localReference, target, closedDelegateType, closedMethodOnTarget));
return localReference;
}

private AssignStatement SetDelegate(LocalReference localDelegate, Reference localTarget,
private AssignStatement SetDelegate(LocalLocation localDelegate, IExpression target,
Type closedDelegateType, MethodInfo closedMethodOnTarget)
{
var delegateCreateDelegate = new MethodInvocationExpression(
null,
DelegateMethods.CreateDelegate,
new TypeTokenExpression(closedDelegateType),
localTarget,
target,
new MethodTokenExpression(closedMethodOnTarget));
return new AssignStatement(localDelegate, new ConvertExpression(closedDelegateType, delegateCreateDelegate));
}
Expand Down
14 changes: 7 additions & 7 deletions src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ internal class MixinContributor : CompositeTypeContributor
{
private readonly bool canChangeTarget;
private readonly IList<Type> empty = new List<Type>();
private readonly IDictionary<Type, FieldReference> fields = new SortedDictionary<Type, FieldReference>(new FieldReferenceComparer());
private readonly GetTargetExpressionDelegate getTargetExpression;
private readonly IDictionary<Type, FieldLocation> fields = new SortedDictionary<Type, FieldLocation>(new FieldReferenceComparer());
private readonly GetTargetExpressionDelegate getTarget;

public MixinContributor(INamingScope namingScope, bool canChangeTarget)
: base(namingScope)
{
this.canChangeTarget = canChangeTarget;
getTargetExpression = BuildGetTargetExpression();
getTarget = BuildGetTargetExpression();
}

public IEnumerable<FieldReference> Fields
public IEnumerable<FieldLocation> Fields
{
get { return fields.Values; }
}
Expand Down Expand Up @@ -101,7 +101,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm
return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
invocation,
getTargetExpression,
getTarget,
overrideMethod,
null);
}
Expand All @@ -114,11 +114,11 @@ private GetTargetExpressionDelegate BuildGetTargetExpression()
}

return (c, m) => new NullCoalescingOperatorExpression(
new AsTypeReference(c.GetField("__target"), m.DeclaringType),
new AsTypeExpression(c.GetField("__target"), m.DeclaringType),
fields[m.DeclaringType]);
}

private FieldReference BuildTargetField(ClassEmitter @class, Type type)
private FieldLocation BuildTargetField(ClassEmitter @class, Type type)
{
var name = "__mixin_" + type.FullName.Replace(".", "_");
return @class.CreateField(namingScope.GetUniqueName(name), type);
Expand Down
Loading
Loading