mcintyre321
unread,Jun 5, 2009, 10:59:13 AM6/5/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to LinFu.Framework
I'm trying to write a Future pattern proxy which lazy loads a value
when it is called. It works (see the first test), but only if I
explicitly cast my create delegate. Is there some way to make the
second test pass without changing it?
internal class Future
{
public static T Create<T>(Func<T> get) where T : class
{
ProxyFactory factory = new ProxyFactory();
var interceptor = new GenericFuture<T>(get);
return factory.CreateProxy<T>(interceptor);
}
internal class GenericFuture<T> : IInterceptor
{
private readonly Func<T> getter;
private object instance;
public GenericFuture(Func<T> getter)
{
this.getter = getter;
}
public object Intercept(InvocationInfo info)
{
var target = instance ?? (instance = getter());
return info.TargetMethod.Invoke(target,
info.Arguments);
}
}
}
public class FutureTests
{
[Fact]
public void TestThatDoesntPass()
{
List<string> list = new List<string>() { "hello" };
IList<string> lazyList = Future.Create(() => list as
IList<string>); //<-- notice the cast
list[0] = "goodbye";
Assert.Equal("goodbye", lazyList[0]);
}
[Fact]
public void TestThatIWantToPassButInsteadThrowsAnException()
{
List<string> list = new List<string>() { "hello" };
IList<string> lazyList = Future.Create(() => list);
list[0] = "goodbye";
Assert.Equal("goodbye", lazyList[0]);
}
}