INotifyPropertyChanged

mauro.sampietro's Avatar

mauro.sampietro

24 Nov, 2017 11:20 AM

I'm using a generic class that is declaring a where constraint as follows:

class MyClass<T> where T : INotifyPropertyChanged

In my case T is of type MyAnotherClass which is enhanced with Postsharp's NotifyPropertyChanged attribute.

Visual Studio cannot compile because MyAnotherClass does not provide implicit conversion to INotifyPropertyChanged [error: CS0311].

How can i fix this?

I'm using PostSharp 4.3.20

  1. Support Staff 1 Posted by PostSharp Techn... on 24 Nov, 2017 03:02 PM

    PostSharp Technologies's Avatar

    Hello,

    this is a fundamental limitation of PostSharp. PostSharp runs as a post-build step, so C# compiler cannot see interfaces implemented by PostSharp and complains with CS0311 error.

    You can implement INotifyPropertyChanged yourself and PostSharp still take cares about property change notification.

    In this example:

    [NotifyPropertyChanged]
    public class InpcClass
    {
        public int P1 { get; set; }
    }
    

    you need to implement INotifyPropertyChanged interface and add OnPropertyChanged method:

    [NotifyPropertyChanged]
    public class InpcClass : INotifyPropertyChanged
    {
        public int P1 { get; set; }
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    PostSharp still calls OnPropertyChanged whenever you change property P1.

    -jakub

  2. Support Staff 2 Posted by PostSharp Techn... on 13 Dec, 2017 02:59 PM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as we believe it was solved. Please feel free to reopen the discussion if you need more help.

    Best regards,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 13 Dec, 2017 02:59 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac