I need to design a class where each of the properties of the class is added and updated in an ObservableDictionary in the class itself.
Here is the code I have come up with.
class Win32_DiskDrive : INotifyPropertyChanged
{
private readonly string _classname = "Win32_DiskDrive";
public string ClassName
{
get
{
return _classname;
}
}
private string _caption;
public string Caption
{
get
{
return _caption;
}
set
{
if ((value != _caption) || (value != null))
{
_caption = value;
UpdateDictionary();
OnPropertyChange();
}
}
}
private string _serialNo;
public string SerialNumber
{
get
{
return _serialNo;
}
set
{
if ((value != _serialNo) || (value != null))
{
_serialNo = value;
UpdateDictionary();
OnPropertyChange();
}
}
}
private IDictionary<string,string> _diskDriveProperties = new ObservableDictionary<String, String>();
public IDictionary<string, string> PropertiesDictionary
{
get
{
return _diskDriveProperties;
}
set
{
_diskDriveProperties = value;
}
}
private void UpdateDictionary([CallerMemberName] string propertyname = null)
{
_diskDriveProperties[propertyname] = this.GetType().GetProperty(propertyname).GetValue(this).ToString();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
Is there a way to implement this class without reflection by using Linq?
Aucun commentaire:
Enregistrer un commentaire