1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 15:20:10 +00:00
msfs-popout-panel-manager/Shared/ObservableObject.cs

76 lines
2.6 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Reflection;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Shared
{
public class ObservableObject : INotifyPropertyChanged
{
// Implements Fody.Changed
public event PropertyChangedEventHandler PropertyChanged;
2023-07-12 22:41:31 +00:00
protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
2022-07-23 19:23:32 +00:00
{
2024-02-28 02:44:21 +00:00
var hasJsonIgnoreAttribute = false;
2023-07-12 22:41:31 +00:00
var type = sender.GetType();
2024-02-28 02:44:21 +00:00
if (e.PropertyName != null)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo != null)
{
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
return;
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
hasJsonIgnoreAttribute = Attribute.IsDefined(propertyInfo, typeof(JsonIgnoreAttribute));
}
2023-07-12 22:41:31 +00:00
}
2024-02-28 02:44:21 +00:00
PropertyChanged?.Invoke(this, new PropertyChangedExtendedEventArgs(e.PropertyName, sender.ToString(), hasJsonIgnoreAttribute));
2023-07-12 22:41:31 +00:00
}
protected void InitializeChildPropertyChangeBinding()
{
2024-02-28 02:44:21 +00:00
var type = this.GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
foreach (var propertyInfo in properties)
2023-07-12 22:41:31 +00:00
{
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
continue;
2024-02-28 02:44:21 +00:00
var childType = propertyInfo.PropertyType;
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
if (!childType.IsSubclassOf(typeof(ObservableObject)))
continue;
var eventInfo = childType.GetEvent("PropertyChanged");
var methodInfo = type.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance);
if (eventInfo == null || eventInfo.EventHandlerType == null || methodInfo == null)
continue;
var dg = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
if(propertyInfo.GetValue(this, null) != null)
2023-07-12 22:41:31 +00:00
eventInfo.AddEventHandler(propertyInfo.GetValue(this, null), dg);
}
2022-07-23 19:23:32 +00:00
}
}
public class PropertyChangedExtendedEventArgs : PropertyChangedEventArgs
{
2023-07-12 22:41:31 +00:00
public virtual bool DisableSave { get; private set; }
2022-07-23 19:23:32 +00:00
public virtual string ObjectName { get; private set; }
public PropertyChangedExtendedEventArgs(string propertyName, string objectName, bool disableSave) : base(propertyName)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
DisableSave = disableSave;
ObjectName = objectName;
2022-07-23 19:23:32 +00:00
}
}
}