How to avoid double construction of proxy with DynamicProxy::CreateClassProxyWithTarget?

Posted by Belvasis on Stack Overflow See other posts from Stack Overflow or by Belvasis
Published on 2012-06-21T08:26:13Z Indexed on 2012/07/01 3:16 UTC
Read the original article Hit count: 170

I am decorating an existing object using the CreateClassProxyWithTarget method. However, the constructor and therefore, initialization code, is being called twice. I already have a "constructed" instance (the target). I understand why this happens, but is there a way to avoid it, other than using an empty constructor?

Edit: Here is some code:

First the proxy creation:

public static T Create<T>(T i_pEntity) where T : class
{
  object pResult = m_pGenerator.CreateClassProxyWithTarget(typeof(T),
                                                           new[] 
                                                             { 
                                                                typeof(IEditableObject),
                                                                typeof(INotifyPropertyChanged) ,
                                                                typeof(IMarkerInterface),
                                                                typeof(IDataErrorInfo)
                                                             },                                                               
                                                           i_pEntity,
                                                           ProxyGenerationOptions.Default,
                                                           new BindingEntityInterceptor<T>(i_pEntity));
  return (T)pResult;
}

I use this for example with an object of the following class:

public class KatalogBase : AuditableBaseEntity
{
   public KatalogBase()
   {
     Values     = new HashedSet<Values>();
     Attributes = new HashedSet<Attributes>();
   }
   ...
}

If i now call BindingFactory.Create(someKatalogBaseObject); the Values and Attributes properties are beeing initialized again.

© Stack Overflow or respective owner

Related posts about c#

Related posts about castle-dynamicproxy