-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to mix in delegate (types) to proxies
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
Showing
10 changed files
with
552 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
272
src/Castle.Core.Tests/DynamicProxy.Tests/DelegateMixinTestCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.