How to avoid double construction of proxy with DynamicProxy::CreateClassProxyWithTarget?
- by Belvasis
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.