Skip to content

Commit

Permalink
Add ability to mix in delegate (types) to proxies
Browse files Browse the repository at this point in the history
This adds the following capabilities to DynamicProxy:

 * `ProxyGenerationOptions.AddDelegateMixin` to add a delegate mixin.
   This will produce an `Invoke` method on the proxy that has the same
   signature as the delegate. The delegate can be `Proceed`-ed to.

 * `ProxyGenerationOptions.AddDelegateTypeMixin` adds an `Invoke`
   method on the proxy that has the same signature as a specified
   delegate type. When those `Invoke` methods are intercepted, they
   cannot be proceeded from as that mixin has no target.

 * `ProxyUtil.TryCreateDelegateToMixin` is a helper method to create a
   delegate of a given type to a suitable `Invoke` method on a proxy.

By combining the above methods, you can now create a delegate proxy:

    var options = new ProxyGenerationOptions();
    options.AddDelegateTypeMixin(typeof(Action));

    var _ = generator.CreateClassProxy(typeof(object), options, ...);

    ProxyUtil.TryCreateDelegateToMixin(_, out Action proxy);
                                          ^^^^^^^^^^^^^^^^
  • Loading branch information
stakx committed Mar 13, 2019
1 parent 680a594 commit c05a8b9
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Enhancements:
- Added trace logging level below Debug; maps to Trace in log4net/NLog, and Verbose in Serilog (@pi3k14, #404)
- Recognize read-only parameters by the `In` modreq (@zvirja, #406)
- DictionaryAdapter: Exposed GetAdapter overloads with NameValueCollection parameter in .NET Standard (@rzontar, #423)
- Ability to add delegate mixins to proxies using `ProxyGenerationOptions.AddDelegate[Type]Mixin`. You can bind to the mixed-in `Invoke` methods on the proxy using `ProxyUtil.TryCreateDelegateToMixin`. (@stakx, #436)

Deprecations:
- The API surrounding `Lock` has been deprecated. This consists of the members listed below. Consider using the Base Class Library's `System.Threading.ReaderWriterLockSlim` instead. (@stakx, #391)
Expand Down
272 changes: 272 additions & 0 deletions src/Castle.Core.Tests/DynamicProxy.Tests/DelegateMixinTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
// Copyright 2004-2010 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests
{
using System;
using System.Reflection;

using NUnit.Framework;

[TestFixture]
public class DelegateMixinTestCase : BasePEVerifyTestCase
{
[Test]
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_null_throws_ArgumentNullException()
{
var options = new ProxyGenerationOptions();
Assert.Throws<ArgumentNullException>(() => options.AddDelegateTypeMixin(null));
}

[Test]
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_non_delegate_type_throws_ArgumentException()
{
var options = new ProxyGenerationOptions();
Assert.Throws<ArgumentException>(() => options.AddDelegateTypeMixin(typeof(Exception)));
}

[Test]
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_delegate_type_succeeds()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));
}

[Test]
public void ProxyGenerationOptions_AddDelegateMixin_when_given_delegate_type_throws_ArgumentNullException()
{
var options = new ProxyGenerationOptions();
Assert.Throws<ArgumentNullException>(() => options.AddDelegateMixin(null));
}

[Test]
public void ProxyGenerationOptions_AddDelegateMixin_when_given_delegate_succeeds()
{
var options = new ProxyGenerationOptions();
options.AddDelegateMixin(new Action(() => { }));
}


[Test]
public void ProxyGenerator_CreateClassProxy_can_create_delegate_proxy_without_target()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));
var _ = new Interceptor();
var proxy = generator.CreateClassProxy(typeof(object), options, _);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_can_create_delegate_proxy_without_target()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));
var _ = new Interceptor();
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, _);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithTarget_can_create_delegate_proxy_without_target()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));
var _ = new Interceptor();
var proxy = generator.CreateInterfaceProxyWithTarget(typeof(IComparable), target, options, _);
}

[Test]
public void ProxyGenerator_CreateClassProxy_can_create_callable_delegate_proxy_without_target()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor();

var proxy = generator.CreateClassProxy(typeof(object), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.AreSame(typeof(Action).GetTypeInfo().GetMethod("Invoke"), interceptor.LastInvocation.Method);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_can_create_callable_delegate_proxy_without_target()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor();

var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.AreSame(typeof(Action).GetTypeInfo().GetMethod("Invoke"), interceptor.LastInvocation.Method);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithTarget_can_create_callable_delegate_proxy_without_target()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor();

var proxy = generator.CreateInterfaceProxyWithTarget(typeof(IComparable), target, options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.AreSame(typeof(Action).GetTypeInfo().GetMethod("Invoke"), interceptor.LastInvocation.Method);
}

[Test]
public void ProxyGenerator_CreateClassProxy_cannot_proceed_to_delegate_type_mixin()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateClassProxy(typeof(object), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

Assert.Throws<NotImplementedException>(() => action.Invoke());
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_cannot_proceed_to_delegate_type_mixin()
{
var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

Assert.Throws<NotImplementedException>(() => action.Invoke());
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithTarget_cannot_proceed_to_delegate_type_mixin()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateTypeMixin(typeof(Action));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateInterfaceProxyWithTarget(typeof(IComparable), target, options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

Assert.Throws<NotImplementedException>(() => action.Invoke());
}

[Test]
public void ProxyGenerator_CreateClassProxy_can_proceed_to_delegate_mixin()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateMixin(new Action(target.Method));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateClassProxy(typeof(object), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.True(target.MethodInvoked);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_can_proceed_to_delegate_mixin()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateMixin(new Action(target.Method));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.True(target.MethodInvoked);
}

[Test]
public void ProxyGenerator_CreateInterfaceProxyWithTarget_can_proceed_to_delegate_mixin()
{
var target = new Target();

var options = new ProxyGenerationOptions();
options.AddDelegateMixin(new Action(target.Method));

var interceptor = new Interceptor(shouldProceed: true);

var proxy = generator.CreateInterfaceProxyWithTarget(typeof(IComparable), target, options, interceptor);
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));

action.Invoke();
Assert.True(target.MethodInvoked);
}

[Serializable]
public sealed class Target : IComparable
{
public bool CompareToInvoked { get; set; }

public bool MethodInvoked { get; set; }

public int CompareTo(object obj)
{
CompareToInvoked = true;
return 123;
}

public void Method()
{
MethodInvoked = true;
}
}

private sealed class Interceptor : IInterceptor
{
private readonly bool shouldProceed;

public Interceptor(bool shouldProceed = false)
{
this.shouldProceed = shouldProceed;
}

public IInvocation LastInvocation { get; set; }

public void Intercept(IInvocation invocation)
{
LastInvocation = invocation;
if (shouldProceed)
{
invocation.Proceed();
}
}
}
}
}
72 changes: 72 additions & 0 deletions src/Castle.Core.Tests/DynamicProxy.Tests/ProxyUtilTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,63 @@ namespace Castle.DynamicProxy.Tests
[TestFixture]
public class ProxyUtilTestCase
{
[Test]
public void TryCreateDelegateToMixin_when_given_null_for_proxy_throws_ArgumentNullException()
{
var _ = typeof(Action);
Assert.Throws<ArgumentNullException>(() => ProxyUtil.TryCreateDelegateToMixin(null, _, out var __));
}

[Test]
public void TryCreateDelegateToMixin_when_given_null_for_delegateType_throws_ArgumentNullException()
{
var _ = new object();
Assert.Throws<ArgumentNullException>(() => ProxyUtil.TryCreateDelegateToMixin(_, null, out var __));
}

[Test]
public void TryCreateDelegateToMixin_when_given_non_delegate_type_throws_ArgumentException()
{
var _ = new object();
Assert.Throws<ArgumentException>(() => ProxyUtil.TryCreateDelegateToMixin(_, typeof(Exception), out var __));
}

[Test]
public void TryCreateDelegateToMixin_when_given_valid_arguments_succeeds()
{
var proxy = new FakeProxyWithInvokeMethods();
Assert.True(ProxyUtil.TryCreateDelegateToMixin(proxy, typeof(Action), out _));
}

[Test]
public void TryCreateDelegateToMixin_returns_false_if_no_suitable_Invoke_method_found()
{
var proxy = new FakeProxyWithInvokeMethods();
Assert.False(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action<bool> boolAction));
}

[Test]
public void TryCreateDelegateToMixin_returns_invokable_delegate()
{
var proxy = new FakeProxyWithInvokeMethods();
Assume.That(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));
action.Invoke();
}

[Test]
public void TryCreateDelegateToMixin_can_deal_with_multiple_Invoke_overloads()
{
var proxy = new FakeProxyWithInvokeMethods();

Assume.That(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action action));
action.Invoke();
Assert.AreEqual("Invoke()", proxy.LastInvocation);

Assume.That(ProxyUtil.TryCreateDelegateToMixin(proxy, out Action<int> intAction));
intAction.Invoke(42);
Assert.AreEqual("Invoke(42)", proxy.LastInvocation);
}

[TestCaseSource(nameof(AccessibleMethods))]
public void IsAccessible_Accessible_Method_Returns_True(MethodBase method)
{
Expand Down Expand Up @@ -123,5 +180,20 @@ public void APublicMethod()
{
}
}

private sealed class FakeProxyWithInvokeMethods
{
public string LastInvocation { get; set; }

public void Invoke()
{
LastInvocation = "Invoke()";
}

public void Invoke(int arg)
{
LastInvocation = $"Invoke({arg})";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options
public void AddInterfaceToProxy(Type @interface)
{
Debug.Assert(@interface != null, "@interface == null", "Shouldn't be adding empty interfaces...");
Debug.Assert(@interface.GetTypeInfo().IsInterface, "@interface.IsInterface", "Should be adding interfaces only...");
Debug.Assert(@interface.GetTypeInfo().IsInterface || @interface.GetTypeInfo().IsSubclassOf(typeof(MulticastDelegate)), "@interface.IsInterface || @interface.IsSubclassOf(typeof(MulticastDelegate))", "Should be adding interfaces or delegate types only...");
Debug.Assert(!interfaces.Contains(@interface), "!interfaces.ContainsKey(@interface)",
"Shouldn't be adding same interface twice...");

Expand Down
Loading

0 comments on commit c05a8b9

Please sign in to comment.