Is it possible to use reflection in CompileTimeInitialize
in PostSharp 3.1?
Following code worked in 3.0:
public class TestClass
{
public string TestField;
[TestAspect]
public void TestMethod() { }
}
public class TestAspect : OnMethodBoundaryAspect
{
private LocationInfo locationInfo;
public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
{
this.locationInfo = new LocationInfo(method.ReflectedType.GetField("TestField"));
}
public override void OnSuccess(MethodExecutionArgs args)
{
Console.WriteLine(this.locationInfo);
}
}
With the 3.1 upgrade, this.locationInfo
becomes Missing Property
and accessing any of its properties cause NullReferenceException
.
Was I doing this a wrong way or has this been changed in 3.1 upgrade? If so, can you suggest me the right way to approach this?
PS: If I set this.locationInfo
in RuntimeInitialize
things work properly.
You can use reflection in CompileTimeInitialize
method and, actually, locationInfo
holds correct information during that method's execution.
However, the locationInfo
field is then serialized, and later deserialized during runtime. This is where the problem happens - apparently, version 3.1 has introduced a bug related to serialization in this particular case. You can check this by saving only locationInfo.Name in the field, for example.
It means you'd need to wait for a bug fix to be implemented in 3.1. You may also want to report the bugs directly on the PostSharp support forum.
Update: The issue has been fixed in PostSharp build 3.1.30.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With