【问题标题】:PostSharp throws error in VS 2015 but not in VS 2013 when buildingPostSharp 在 VS 2015 中抛出错误,但在 VS 2013 中没有
【发布时间】:2016-02-17 17:08:05
【问题描述】:

我有一个包含多个项目的 WPF 企业级解决方案。代码在 VS 2013 中构建良好,在 VS 2015 中代码无法构建。

我试过了:

  1. 删除受影响的项目并调整代码以观察它就是这个项目。是的。
  2. 我尝试使用 NuGet 管理器删除“PostSharp”、“PostSharp.Patterns.Common”、“PostSharp.Patterns.Model”的 PostSharp 引用;重建,重新启动 VS,从 Nuget 获取最新信息。同样的例外。
  3. 我尝试升级所有其他项目。同样的故事。

在解决方案中,我还提到了 MS 的“Prism”和“Unity”。问题是我什至不知道这发生在“哪里”,只是因为堆栈跟踪错误只是提到了项目而不是特定的类或位置,而不是对“PostSharp”命名空间的引用。是这样的:

'Severity   Code    Description Project File    Line    Suppression State
Error       An unexpected exception occurred when executing user code: System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeConstructorInfo' to type 'System.Reflection.MethodInfo'.
   at PostSharp.Patterns.Model.NotifyPropertyChanged.DependencyAnalysis.PropertiesDependenciesAnalyzer.MethodAnalyzer.TryGetDependsOn(IMethodCallExpression expression, List`1& invocationPaths, List`1& messages)
   at PostSharp.Patterns.Model.NotifyPropertyChanged.DependencyAnalysis.PropertiesDependenciesAnalyzer.MethodAnalyzer.VisitMethodCallExpression(IMethodCallExpression expression)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitSyntaxElement(ISyntaxElement syntaxElement)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitStatementExpression(IStatementExpression statementExpression)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitSyntaxElement(ISyntaxElement syntaxElement)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitBlockExpression(IBlockExpression instructionBlock)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitSyntaxElement(ISyntaxElement syntaxElement)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitBlockExpression(IBlockExpression instructionBlock)
   at PostSharp.Reflection.Syntax.SyntaxTreeVisitor.VisitMethodBody(ISyntaxMethodBody methodBody)
   at PostSharp.Patterns.Model.NotifyPropertyChanged.DependencyAnalysis.PropertiesDependenciesAnalyzer.MethodAnalyzer.AnalyzeMethodRecursive(MethodBase method)
   at PostSharp.Patterns.Model.NotifyPropertyChanged.DependencyAnalysis.PropertiesDependenciesAnalyzer.MethodAnalyzer.AnalyzeProperty(Type type, PropertyInfo propertyInfo, ExplicitDependencyMap currentTypeExplicitDependencyMap)
   at PostSharp.Patterns.Model.NotifyPropertyChanged.DependencyAnalysis.PropertiesDependenciesAnalyzer.AnalyzeType(Type type)
   at PostSharp.Patterns.Model.NotifyPropertyChangedAttribute.CompileTimeInitialize(Type type, AspectInfo aspectInfo)
   at PostSharp.Sdk.AspectWeaver.AspectWeavers.TypeLevelAspectWeaverInstance.^RDzfO2to.^XTF+fjOj()
   at PostSharp.Sdk.Utilities.ExceptionHelper.ExecuteUserCode(MessageLocation messageLocation, Action userCode, Type[] acceptableExceptions).   Namespace.Project D:\TFS\SourceCodeLocation\POSTSHARP

【问题讨论】:

    标签: visual-studio-2013 visual-studio-2015 postsharp


    【解决方案1】:

    事实证明,即使将“严格”的编译选项设置为打开,VS 2013 和 2015 也允许这样做非常简单,但 PostSharp 不会。

    Private _DateDeactivated As Nullable(Of Date)
    
    <SafeForDependencyAnalysis>
    Public Property DateDeactivated As Nullable(Of Date)
      Get
        If MyRequest.DateDeactivated.HasValue Then
          IsDeactivated = True
            Return MyRequest.DateDeactivated.Value
          Else
            Return New Nullable(Of Date)
          End If
        End Get
      Set(value As Nullable(Of Date))
        _DateDeactivated = value
      End Set
    End Property
    

    这一行

    Return MyRequest.DateDeactivated.Value
    

    应该是:

    Return MyRequest.DateDeactivated
    

    这不是我的代码,而是导致更大的解决方案无法在 VS 2015 中使用 PostSharp 进行编译。我更担心 PostSharp 无法提供对实际文件的引用。我通过浏览文件并注释掉属性和方法直到构建成功,然后浏览文件直到发现问题所在,才发现了这一点。可能有更好的验证工具,例如 ReSharper,它可能已经捕获了它,但它确实有效。这可能对某人有帮助,也可能没有。

    【讨论】:

      【解决方案2】:

      这是[NotifyPropertyChanged] 方面的一个错误,应该在版本 4.2.26 和 4.3.9 中修复。如果不是这样,请告诉我。

      问题在于 VB 编译器生成用于处理 Nullable&lt;T&gt; 的 IL 代码的方式 - 对属性依赖关系的分析根本没有预料到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 2015-12-28
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多