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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -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 Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
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
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -25,12 +25,12 @@ namespace Castle.DynamicProxy.Contributors
/// </summary>
internal sealed class ProxyTargetAccessorContributor : ITypeContributor
{
private readonly Func<Reference> getTargetReference;
private readonly Func<IExpression> getTarget;
private readonly Type targetType;

public ProxyTargetAccessorContributor(Func<Reference> getTargetReference, Type targetType)
public ProxyTargetAccessorContributor(Func<IExpression> getTarget, Type targetType)
{
this.getTargetReference = getTargetReference;
this.getTarget = getTarget;
this.targetType = targetType;
}

Expand All @@ -41,17 +41,17 @@ public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)
public void Generate(ClassEmitter emitter)
{
var interceptorsField = emitter.GetField("__interceptors");
var targetReference = getTargetReference();
var target = getTarget();

var dynProxyGetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxyGetTarget), typeof(object));

dynProxyGetTarget.CodeBuilder.AddStatement(
new ReturnStatement(new ConvertExpression(typeof(object), targetType, targetReference)));
new ReturnStatement(new ConvertExpression(typeof(object), targetType, target)));

var dynProxySetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxySetTarget), typeof(void), typeof(object));

// we can only change the target of the interface proxy
if (targetReference is FieldReference targetField)
if (target is FieldReference targetField)
{
dynProxySetTarget.CodeBuilder.AddStatement(
new AssignStatement(targetField,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,7 +51,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco
protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor()
{
return new ProxyTargetAccessorContributor(
getTargetReference: () => SelfReference.Self,
getTarget: () => ThisExpression.Instance,
targetType);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,7 +65,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco
protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor()
{
return new ProxyTargetAccessorContributor(
getTargetReference: () => targetField,
getTarget: () => targetField,
targetType);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,7 +62,7 @@ protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invoca
{
invokeMethodOnTarget.CodeBuilder.AddStatement(
new MethodInvocationExpression(
SelfReference.Self,
ThisExpression.Instance,
InvocationMethods.CompositionInvocationEnsureValidTarget));

base.ImplementInvokeMethodOnTarget(invocation, parameters, invokeMethodOnTarget, targetField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Reflection.Emit;

[DebuggerDisplay("argument {Type}")]
internal class ArgumentReference : TypeReference
internal class ArgumentReference : Reference
{
public ArgumentReference(Type argumentType)
: base(argumentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class AsTypeReference : Reference
private readonly Type type;

public AsTypeReference(Reference reference, Type type)
: base(type)
{
this.reference = reference;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST


[DebuggerDisplay("&{localReference}")]
internal class ByRefReference : TypeReference
internal class ByRefReference : Reference
{
private readonly LocalReference localReference;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class FieldReference : Reference
private readonly bool isStatic;

public FieldReference(FieldInfo field)
: base(field.FieldType)
{
this.field = field;
if ((field.Attributes & FieldAttributes.Static) != 0)
Expand All @@ -37,6 +38,7 @@ public FieldReference(FieldInfo field)
}

public FieldReference(FieldBuilder fieldBuilder)
: base(fieldBuilder.FieldType)
{
this.fieldBuilder = fieldBuilder;
field = fieldBuilder;
Expand Down Expand Up @@ -64,7 +66,7 @@ public override void EmitAddress(ILGenerator gen)
}
else
{
SelfReference.Self.Emit(gen);
ThisExpression.Instance.Emit(gen);
gen.Emit(OpCodes.Ldflda, Reference);
}
}
Expand All @@ -77,7 +79,7 @@ public override void Emit(ILGenerator gen)
}
else
{
SelfReference.Self.Emit(gen);
ThisExpression.Instance.Emit(gen);
gen.Emit(OpCodes.Ldfld, Reference);
}
}
Expand All @@ -91,7 +93,7 @@ public override void EmitStore(IExpression value, ILGenerator gen)
}
else
{
SelfReference.Self.Emit(gen);
ThisExpression.Instance.Emit(gen);
value.Emit(gen);
gen.Emit(OpCodes.Stfld, Reference);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
/// ByRef and provides indirect load/store support.
/// </summary>
[DebuggerDisplay("&{OwnerReference}")]
internal class IndirectReference : TypeReference
internal class IndirectReference : Reference
{
private readonly TypeReference byRefReference;
private readonly Reference byRefReference;

public IndirectReference(TypeReference byRefReference)
: base(byRefReference.Type.GetElementType())
public IndirectReference(Reference byRefReference)
: base(byRefReference.Type.GetElementType()!)
{
if (!byRefReference.Type.IsByRef)
{
Expand Down Expand Up @@ -59,15 +59,15 @@ public override void EmitStore(IExpression value, ILGenerator gen)
OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, Type);
}

public static TypeReference WrapIfByRef(TypeReference reference)
public static Reference WrapIfByRef(Reference reference)
{
return reference.Type.IsByRef ? new IndirectReference(reference) : reference;
}

// TODO: Better name
public static TypeReference[] WrapIfByRef(TypeReference[] references)
public static Reference[] WrapIfByRef(Reference[] references)
{
var result = new TypeReference[references.Length];
var result = new Reference[references.Length];

for (var i = 0; i < references.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Reflection.Emit;

[DebuggerDisplay("local {Type}")]
internal class LocalReference : TypeReference
internal class LocalReference : Reference
{
private LocalBuilder? localBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ internal class MethodInvocationExpression : IExpression, IStatement
{
protected readonly IExpression[] args;
protected readonly MethodInfo method;
protected readonly Reference? owner;
protected readonly IExpression? instance;

public MethodInvocationExpression(MethodInfo method, params IExpression[] args) :
this(SelfReference.Self, method, args)
this(ThisExpression.Instance, method, args)
{
}

public MethodInvocationExpression(MethodEmitter method, params IExpression[] args) :
this(SelfReference.Self, method.MethodBuilder, args)
this(ThisExpression.Instance, method.MethodBuilder, args)
{
}

public MethodInvocationExpression(Reference? owner, MethodEmitter method, params IExpression[] args) :
this(owner, method.MethodBuilder, args)
public MethodInvocationExpression(IExpression? instance, MethodEmitter method, params IExpression[] args)
: this(instance, method.MethodBuilder, args)
{
}

public MethodInvocationExpression(Reference? owner, MethodInfo method, params IExpression[] args)
public MethodInvocationExpression(IExpression? instance, MethodInfo method, params IExpression[] args)
{
this.owner = owner;
this.instance = instance;
this.method = method;
this.args = args;
}
Expand All @@ -51,7 +51,7 @@ public MethodInvocationExpression(Reference? owner, MethodInfo method, params IE

public void Emit(ILGenerator gen)
{
owner?.Emit(gen);
instance?.Emit(gen);

foreach (var exp in args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@

namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
{
using System;
using System.Reflection.Emit;

internal abstract class Reference : IExpression
{
private readonly Type type;

protected Reference(Type type)
{
this.type = type;
}

public Type Type
{
get { return type; }
}

public abstract void EmitAddress(ILGenerator gen);

public abstract void Emit(ILGenerator gen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST

internal class ReferencesToObjectArrayExpression : IExpression
{
private readonly TypeReference[] args;
private readonly Reference[] args;

public ReferencesToObjectArrayExpression(params TypeReference[] args)
public ReferencesToObjectArrayExpression(params Reference[] args)
{
this.args = args;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,17 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Reflection.Emit;

[DebuggerDisplay("this")]
internal class SelfReference : Reference
internal class ThisExpression : IExpression
{
public static readonly SelfReference Self = new SelfReference();
public static readonly ThisExpression Instance = new ThisExpression();

protected SelfReference()
protected ThisExpression()
{
}

public override void EmitAddress(ILGenerator gen)
{
throw new NotSupportedException();
}

public override void Emit(ILGenerator gen)
public void Emit(ILGenerator gen)
{
gen.Emit(OpCodes.Ldarg_0);
}

public override void EmitStore(IExpression value, ILGenerator gen)
{
// It does not make sense to assign a value to the this pointer.
throw new NotSupportedException();
}
}
}

This file was deleted.

Loading
Loading