C# 反射能否用于PostSharp 3.1中的CompileTimeInitialize?
是否可以在PostSharp 3.1的C# 反射能否用于PostSharp 3.1中的CompileTimeInitialize?,c#,postsharp,C#,Postsharp,是否可以在PostSharp 3.1的CompileTimeInitialize中使用反射 以下代码在3.0中工作: public class TestClass { public string TestField; [TestAspect] public void TestMethod() { } } public class TestAspect : OnMethodBoundaryAspect { private LocationInfo locatio
CompileTimeInitialize
中使用反射
以下代码在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);
}
}
在3.1升级中,this.locationInfo
变为缺少属性
,访问其任何属性都会导致NullReferenceException
我这样做是错误的还是在3.1升级中有所改变?
如果是这样,你能给我建议正确的方法吗
PS:如果我在
RuntimeInitialize
中设置了this.locationInfo
,事情就会正常运行。您可以在CompileTimeInitialize
方法中使用反射,实际上,locationInfo
在该方法执行期间保存正确的信息
但是,locationInfo
字段随后被序列化,然后在运行时反序列化。这就是问题发生的地方——显然,3.1版在这个特殊情况下引入了一个与序列化相关的bug。例如,您可以通过在字段中仅保存locationInfo.Name来检查这一点
这意味着您需要等待在3.1中实现bug修复。您可能还希望直接在上报告错误
更新:该问题已在PostSharp build 3.1.30中修复。谢谢。现在我知道这对我来说不是问题。我将向PostSharp论坛报告这一点。