waoywssy / linfu

Automatically exported from code.google.com/p/linfu
0 stars 0 forks source link

The generated default constructor in the ProxyFactory.CreateProxyType type will always call Object's constructor, instead of default constructor of the base type #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In CreateProxyType the call 

proxyType.AddDefaultConstructor() should probably be
proxyType.AddDefaultConstructor(actualBaseType) 

otherwise code skips the constructor in the passed base class! 

This error occurs at least in 2.2.0.

Following Code demonstrates this:

using System;
using LinFu.AOP.Interfaces;
using LinFu.Proxy;
using LinFu.Proxy.Interfaces;
using NUnit.Framework;

namespace gcache.tests.TestEnvironment
{
  public class A
  {
    public bool ConstructorRan;
    public A()
    {
      ConstructorRan = true;
    }
  }

    [TestFixture]
  public class TestLinfu
  {
      [Test]
    public void Test()
    {
      ProxyFactory pf = new ProxyFactory();

      A proxy = (A) pf.CreateProxy(typeof(A), (IInterceptor)null, new Type[0]);
      Assert.IsTrue(proxy.ConstructorRan);
    }
  }
}

Original issue reported on code.google.com by dirk.bo...@gmail.com on 18 Dec 2009 at 2:06

GoogleCodeExporter commented 9 years ago
Hi dirk,

Calling the object constructor is done by design so that the proxy won't have 
the
state (or baggage) of the real object. Each proxy instance is intentionally 
designed
to have no state of its own (aside from the pointer to the interceptor type 
that will
handle all of its calls), and skipping the base class constructor call uniformly
allows LinFu.Proxy v2.2 to treat interfaces and abstract base class method 
calls the
same way with the same interceptor, regardless if the base type is an interface 
or an
abstract class--so I'm afraid I can't this one...

Original comment by Philip.L...@gmail.com on 18 Dec 2009 at 2:37

GoogleCodeExporter commented 9 years ago
Ok fine, a pity though. I wasn't using the library to build a proxy really. I 
was
"injecting" interfaces into a class, implementing the behaviour of those 
interfaces
in terms of that class. I'll solve it another way.

best regards,
Dirk

Original comment by dirk.bo...@gmail.com on 18 Dec 2009 at 9:57