Skip to content

Instantly share code, notes, and snippets.

@emoacht
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoacht/b3fc16f6584c79164552 to your computer and use it in GitHub Desktop.
Save emoacht/b3fc16f6584c79164552 to your computer and use it in GitHub Desktop.
CallerMemberNameAttribute for .NET Framework earlier than 4.5
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerMemberNameAttribute : Attribute
{
}
}
Namespace Global.System.Runtime.CompilerServices
<AttributeUsage(AttributeTargets.Parameter, AllowMultiple:=False, Inherited:=False)>
Public Class CallerMemberNameAttribute
Inherits Attribute
End Class
End Namespace
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public abstract class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(ISynchronizeInvoke invoker, [CallerMemberName] string propertyName = null)
{
if ((invoker != null) && invoker.InvokeRequired)
{
invoker.Invoke(new Action(() => RaisePropertyChanged(propertyName)), null);
}
else
{
RaisePropertyChanged(propertyName);
}
}
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public MustInherit Class NotificationObject
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub RaisePropertyChanged(invoker As ISynchronizeInvoke, <CallerMemberName> Optional propertyName As String = Nothing)
If ((invoker IsNot Nothing) AndAlso invoker.InvokeRequired) Then
invoker.Invoke(Sub() RaisePropertyChanged(propertyName), Nothing)
Else
RaisePropertyChanged(propertyName)
End If
End Sub
Protected Overridable Sub RaisePropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment