postsharp / PostSharp.IL

PostSharp.IL is an aspect-oriented framework based on IL rewriting. Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
2 stars 1 forks source link

Post.Cast creates InvalidCastException #3

Closed cfHxqA closed 1 year ago

cfHxqA commented 1 year ago

The example code mentioned here works with version 2023.0.7 and previous. But NOT with the current version 2023.0.8. Message: System.InvalidCastException: "Unable to cast object of type 'ConsoleApp51.Test' to type 'System.ComponentModel.INotifyPropertyChanged'."

[NotifyPropertyChanged]
public class Test {
  public Test() {
    Post.Cast<Test, INotifyPropertyChanged>(this).PropertyChanged += Test_PropertyChanged;
  }

  private void Test_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
  }
}
PostSharpBot commented 1 year ago

Hello @cfHxqA, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-33949.

addabis commented 1 year ago

Thanks for contacting us.

I was not able to reproduce the issue. Can you please include a reproduction project?

cfHxqA commented 1 year ago

The example code mentioned here works with version 2023.0.7 and previous. But NOT with the current version 2023.0.8. Message: System.InvalidCastException: "Unable to cast object of type 'ConsoleApp51.Test' to type 'System.ComponentModel.INotifyPropertyChanged'."

[NotifyPropertyChanged]
public class Test {
  public Test() {
    Post.Cast<Test, INotifyPropertyChanged>(this).PropertyChanged += Test_PropertyChanged;
  }

  private void Test_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
  }
}

Apparently NotifyPropertyChanged doesn't declare all necessary variables. I'll can solve this issue with extending class with : INotifyPropertyChanged and add public event PropertyChangedEventHandler PropertyChanged;.

cfHxqA commented 1 year ago

Thanks for contacting us.

I was not able to reproduce the issue. Can you please include a reproduction project?

It's possible with new project, project-file content:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PostSharp.Patterns.Model" Version="2023.0.8" />
  </ItemGroup>
</Project>

Program.cs:

using PostSharp;
using PostSharp.Patterns.Model;
using System.ComponentModel;

namespace ConsoleApp51
{
  internal class Program
  {
    static void Main(string[] args)
    {
      var test = new Test();
      Console.ReadLine();
    }
  }

  [NotifyPropertyChanged]
  public class Test {
    public Test() {
      Post.Cast<Test, INotifyPropertyChanged>(this).PropertyChanged += Test_PropertyChanged;
    }

    private void Test_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
    }
  }
}
cfHxqA commented 1 year ago

Thanks for contacting us.

I was not able to reproduce the issue. Can you please include a reproduction project?

Same issue on dotnetfiddle. :-) https://dotnetfiddle.net/oWHnBs

PostSharpBot commented 1 year ago

Hello @cfHxqA, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-33950.

addabis commented 1 year ago

My sample project is virtually the same. If you get the runtime exception it most likely means that PostSharp did not transform the assembly. Can you upload a MSBuild diag log (use /v:diag while building)?

I would not expect dotnetfiddle to run PostSharp, most likely it will only execute the output of the C# compiler, which will not have the aspect applied.

cfHxqA commented 1 year ago

According to the log, everything is created correctly, with the previous version it works wonderfully - but no longer with the new version? I can also provide you with the entire project file.

I can recreate the problem again and again, with new projects or other devices as often as I like. :( I'm also assuming that it is NOT transformed. But there was no change in Visual Studio because I can easily create and test it with the previous version?

Download Project

addabis commented 1 year ago

The build and runtime of the sample you provided passes (for both PostSharp versions). Can you upload the diag build log so that we can verify it?

cfHxqA commented 1 year ago

The build and runtime of the sample you provided passes (for both PostSharp versions). Can you upload the diag build log so that we can verify it?

I can also create the file, that's no problem. The problem is that the necessary functions/variables are NOT added via the attribute and creates listen issue with invalidcast-exception which im already posted.

Binlog file

addabis commented 1 year ago

Currently, I don't understand why this can happen. I can only guess that it is something related to your machine.

It's apparent from the binlog that PostSharp ran, but since it's missing most details of targets and tasks (not sure how you trimmed it) I cannot determine much more.

Can you also enable the following PostSharp trace categories? /p:PostSharpTrace="AspectWeaver;AspectInfrastructure;ProjectLoader;NpcSerialization" (if you're using PowerShell, escape the double quotes)

Please ensure that the output from PostSharpCompile task is included in the log. I'm interested in trace lines looking like this. The full extent of the binlog would be better though:

/home/dev/Support/33949/ConsoleApp1/ConsoleApp1.csproj : info ProjectLoader: Executing task {Globals}.
/home/dev/Support/33949/ConsoleApp1/ConsoleApp1.csproj : info AspectInfrastructure: Processing branch with index=0.
/home/dev/Support/33949/ConsoleApp1/ConsoleApp1.csproj : info AspectWeaver: Providing aspect transformations.
/home/dev/Support/33949/ConsoleApp1/ConsoleApp1.csproj : info NpcSerialization: Type( ConsoleApp1.Test ).

Thanks.

cfHxqA commented 1 year ago

I installed everything again, a fresh installation of Visual Studio, PostSharp and now it works all at once...

I think there was an error while reinstalling or replacing the ZIP archive or replaced incorrectly. I always update from the ZIP archive and not the binary files. Maybe that was the problem this time. 😥😅

Thank you for your patience and sorry!

addabis commented 1 year ago

No problem.