Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反射能否用于PostSharp 3.1中的CompileTimeInitialize?_C#_Postsharp - Fatal编程技术网

C# 反射能否用于PostSharp 3.1中的CompileTimeInitialize?

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

是否可以在PostSharp 3.1的
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论坛报告这一点。