Created
May 29, 2019 12:40
-
-
Save boronology/68372cc5baf50a73b68efd6f2c0ed9ba to your computer and use it in GitHub Desktop.
INotifyPropertyChangedを極力隠蔽する実験の産物。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Collections; | |
namespace SampleProject | |
{ | |
public class ViewModelBase : INotifyPropertyChanged | |
{ | |
private Hashtable properties; | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected ViewModelBase() | |
{ | |
this.properties = new Hashtable(); | |
} | |
protected void SetProperty<T>(T value, [CallerMemberName] string propertyName = "") | |
{ | |
properties[propertyName] = value; | |
} | |
protected T GetProperty<T>([CallerMemberName] string propertyName = "", RequireClass<T> require = null) where T : class | |
{ | |
return properties[propertyName] as T; | |
} | |
protected T GetProperty<T>([CallerMemberName] string propertyName = "", RequireStruct<T> require = null) where T : struct | |
{ | |
var property = properties[propertyName]; | |
try | |
{ | |
return (T)property; | |
} | |
catch | |
{ | |
return default(T); | |
} | |
} | |
protected void OnPropertyChanged([CallerMemberName] string name = "") | |
{ | |
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name)); | |
} | |
} | |
//Genericsでstructとclassでオーバーロードするためのハック | |
public class RequireStruct<T> where T : struct { } | |
public class RequireClass<T> where T : class { } | |
//使用サンプル | |
public class SampleVM : ViewModelBase | |
{ | |
string Title | |
{ | |
get { return GetProperty<string>(); } //型指定が必要なのがつらい | |
set { SetProperty(value); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment