Skip to content

Instantly share code, notes, and snippets.

@linnet8989
Last active April 21, 2018 13:33
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 linnet8989/15de881de2191ee961f0de1d6f5fa81f to your computer and use it in GitHub Desktop.
Save linnet8989/15de881de2191ee961f0de1d6f5fa81f to your computer and use it in GitHub Desktop.
c# get set Property auto Generation and Model class Template (CallerMemberName requires .net 4.5 and higher) for WPF
# -*- coding: UTF-8 -*-\
s = " \
string Name; \
int Number; \
"
s2 = "\
private %s m_%s;\n\
public %s %s\n\
{\n\
get => m_%s;\n\
set => SetProperty(ref m_%s, value);\n\
}\n\
"
for s3 in s.split(";"):
if s3.strip() and s3.strip()[0] != '/':
l = s3.strip().split()
s4 = l[-1]
if cmp(l[0], "string") == 0:
s4 += ' = ""'
print s2 % (l[0], s4, l[0], l[-1], l[-1], l[-1])
public class MyModel : INotifyPropertyChanged
{
//paste your Generated code here
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] string propertyName = null)
{
if (Equals(storage, value)) { return false; }
storage = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment