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
70 changes: 1 addition & 69 deletions src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs
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 @@ -106,73 +106,5 @@ public void CreateStaticFieldWithAttributes()
Assert.IsNotNull(field);
Assert.AreEqual(FieldAttributes.Static | FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes);
}

[Test]
public void UsingClassEmitterForInterfaces()
{
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
emitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
typeof(void), Type.EmptyTypes);
Type t = emitter.BuildType();
Assert.IsTrue(t.IsInterface);
MethodInfo method = t.GetMethod("MyMethod");
Assert.IsNotNull(method);
}

[Test]
public void NoBaseTypeForInterfaces()
{
DisableVerification();
ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);

Assert.Throws<InvalidOperationException>(delegate {
#pragma warning disable 219
Type t = emitter.BaseType;
#pragma warning restore 219
});
}

[Test]
public void NoDefaultCtorForInterfaces()
{
DisableVerification();
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);

Assert.Throws<InvalidOperationException>(delegate {
emitter.CreateDefaultConstructor();
});
}

[Test]
public void NoCustomCtorForInterfaces()
{
DisableVerification();
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);

Assert.Throws<InvalidOperationException>(delegate {
emitter.CreateConstructor();
});
}

[Test]
public void NestedInterface()
{
ClassEmitter outerEmitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IOuter", null, Type.EmptyTypes,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
NestedClassEmitter innerEmitter = new NestedClassEmitter(outerEmitter, "IInner",
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.NestedPublic, null, Type.EmptyTypes);
innerEmitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
typeof(void), Type.EmptyTypes);
Type inner = innerEmitter.BuildType();
Type outer = outerEmitter.BuildType();
Assert.IsTrue(inner.IsInterface);
MethodInfo method = inner.GetMethod("MyMethod");
Assert.IsNotNull(method);
Assert.AreSame(inner, outer.GetNestedType("IInner", BindingFlags.Public));
}
}
}
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 @@ -21,11 +21,11 @@ namespace Castle.DynamicProxy.Contributors

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

MethodInfo GetCallbackMethod();

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

IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy);
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 @@ -40,7 +40,7 @@ public InvocationWithDelegateContributor(Type delegateType, Type targetType, Met
this.namingScope = namingScope;
}

public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, ClassEmitter invocation)
{
var arguments = GetArguments(baseCtorArguments);
var constructor = invocation.CreateConstructor(arguments);
Expand All @@ -55,7 +55,7 @@ public MethodInfo GetCallbackMethod()
return delegateType.GetMethod("Invoke");
}

public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
public MethodInvocationExpression GetCallbackMethodInvocation(ClassEmitter invocation, IExpression[] args,
Reference targetField,
MethodEmitter invokeMethodOnTarget)
{
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 @@ -39,7 +39,7 @@ public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod me
this.targetReference = targetReference;
}

public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, ClassEmitter invocation)
{
return invocation.CreateConstructor(baseCtorArguments);
}
Expand All @@ -49,7 +49,7 @@ public MethodInfo GetCallbackMethod()
return delegateType.GetMethod("Invoke");
}

public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
public MethodInvocationExpression GetCallbackMethodInvocation(ClassEmitter invocation, IExpression[] args,
Reference targetField,
MethodEmitter invokeMethodOnTarget)
{
Expand All @@ -62,7 +62,7 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments,
return arguments;
}

private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
private Reference GetDelegate(ClassEmitter invocation, MethodEmitter invokeMethodOnTarget)
{
var genericTypeParameters = invocation.GenericTypeParams.AsTypeArray();
var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override FieldReference GetTargetReference()
return new FieldReference(InvocationMethods.CompositionInvocationTarget);
}

protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters,
protected override void ImplementInvokeMethodOnTarget(ClassEmitter invocation, ParameterInfo[] parameters,
MethodEmitter invokeMethodOnTarget, Reference targetField)
{
invokeMethodOnTarget.CodeBuilder.AddStatement(
Expand Down
14 changes: 7 additions & 7 deletions src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs
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 @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Internal;

internal class DelegateTypeGenerator : IGenerator<AbstractTypeEmitter>
internal class DelegateTypeGenerator : IGenerator<ClassEmitter>
{
private const TypeAttributes DelegateFlags = TypeAttributes.Class |
TypeAttributes.Public |
Expand All @@ -38,22 +38,22 @@ public DelegateTypeGenerator(MetaMethod method, Type targetType)
this.targetType = targetType;
}

public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScope)
public ClassEmitter Generate(ClassEmitter @class, INamingScope namingScope)
{
var emitter = GetEmitter(@class, namingScope);
BuildConstructor(emitter);
BuildInvokeMethod(emitter);
return emitter;
}

private void BuildConstructor(AbstractTypeEmitter emitter)
private void BuildConstructor(ClassEmitter emitter)
{
var constructor = emitter.CreateConstructor(new ArgumentReference(typeof(object)),
new ArgumentReference(typeof(IntPtr)));
constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
}

private void BuildInvokeMethod(AbstractTypeEmitter @delegate)
private void BuildInvokeMethod(ClassEmitter @delegate)
{
var paramTypes = GetParamTypes(@delegate);
var invoke = @delegate.CreateMethod("Invoke",
Expand All @@ -66,7 +66,7 @@ private void BuildInvokeMethod(AbstractTypeEmitter @delegate)
invoke.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
}

private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingScope)
private ClassEmitter GetEmitter(ClassEmitter @class, INamingScope namingScope)
{
var methodInfo = method.MethodOnTarget;
var suggestedName = string.Format("Castle.Proxies.Delegates.{0}_{1}",
Expand All @@ -84,7 +84,7 @@ private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingS
return @delegate;
}

private Type[] GetParamTypes(AbstractTypeEmitter @delegate)
private Type[] GetParamTypes(ClassEmitter @delegate)
{
var parameters = method.MethodOnTarget.GetParameters();
if (@delegate.TypeBuilder.IsGenericType)
Expand Down
Loading
Loading