Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new methods ProxyGenerator.CreateDelegateProxy[WithTarget] #403

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

Enhancements:
- New methods `ProxyGenerator.CreateDelegateProxy[WithTarget]` for creating delegate proxies. (@stakx, #403)

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)
- `Castle.Core.Internal.Lock` (class)
Expand Down
2 changes: 2 additions & 0 deletions docs/dynamicproxy-kinds-of-proxy-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Composition-based proxy is a new object that inherits from proxied class/impleme

* Interface proxy with target interface - this kind of proxy is kind of a hybrid of two other interface proxy kinds. It allows, but not requires target object to be supplied. It also allows the target to be swapped during the lifetime of the proxy. It is not tied to one type of the proxy target so one proxy type can be reused for different target types as long as they implement the target interface.

* Delegate proxy with and without target - this proxy kind targets delegates, as the name implies. DynamicProxy builds a delegate that, when invoked, in turn invokes all configured interceptors and/or the target delegate. At least one interceptor or a target must be specified in order for the generated delegate to have an implementation.

## External resources

* [Tutorial on DynamicProxy discussing with examples all kinds of proxies](http://kozmic.pl/dynamic-proxy-tutorial/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Tests.Interceptors;
using NUnit.Framework;

[TestFixture]
public class DelegateProxyTestCasE : BasePEVerifyTestCase
{
private Type GenerateProxyType<TDelegate>()
{
var scope = generator.ProxyBuilder.ModuleScope;
var proxyGenerator = new DelegateProxyGenerator(scope, typeof (TDelegate))
{
Logger = generator.ProxyBuilder.Logger
};
return proxyGenerator.GetProxyType();
}

private T GetProxyInstance<T>(T func, params IInterceptor[] interceptors)
{
var type = GenerateProxyType<T>();
var instance = Activator.CreateInstance(type, func, interceptors);
#if FEATURE_LEGACY_REFLECTION_API
return (T) (object) Delegate.CreateDelegate(typeof (T), instance, "Invoke");
#else
var methodInfo = instance.GetType().GetMethod("Invoke");
return (T)(object)methodInfo.CreateDelegate(typeof(T), instance);
#endif
}

[Test]
public void Can_create_Delegate_type_proxy()
{
var type = GenerateProxyType<Func<int>>();
Assert.IsNotNull(type);
}

[Test]
public void Can_intercept_call_to_delegate()
{
var proxy = GetProxyInstance<Func<int>>(() =>
{
Assert.Fail("Shouldn't have gone that far");
return 5;
}, new SetReturnValueInterceptor(3));
var result = proxy.Invoke();
Assert.AreEqual(3, result);
}

[Test]
public void Can_intercept_call_to_delegate_no_target()
{
var proxy = GetProxyInstance<Func<int>>(null, new SetReturnValueInterceptor(3));
var result = proxy.Invoke();
Assert.AreEqual(3, result);
}

[Test]
public void Can_proxy_delegate_with_no_target()
{
var proxy = GetProxyInstance<Func<int>>(() => 5);
var result = proxy.Invoke();
Assert.AreEqual(5, result);
}
}
}
Loading