[PostSharp.Aspects.Advices.OnInstanceConstructedAdvice]
public void OnInstanceConstructed()
{
var instanceType = this.Instance.GetType();
if (this.appliedTo != instanceType)
{
var LoadInDbMethod = instanceType.GetMethod("GetObjectInDatabase", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var objInDb = LoadInDbMethod.Invoke(this.Instance, new object[] { null, true });
if (objInDb != null)
{
this.Instance = objInDb;
//postshot throws an error that this is readonly!!
}
}
}
PostSharp doesn't let me to change instance once instance is created but I need a way to assign another object to this instance after it's constructed!
Suppose I have following code
var Pejo = new Car(){ Name = "405"};
Pejo 405 exist in my database with all of it's properties so I want Pejo to be my database object not a new object with null properties(other than Name).
I can also move from PostSharp to any other C# AOP framework that let me to do this.
How to achieve this with PostSharp or any other AOP framework ?
It is not possible to do this in the current version of PostSharp (4.0 at the time of writing). You actually would need to intercept the call to the constructor, which is currently unsupported.
A solution now is to use a factory method and implement the logic there.