📄 propertydictionary.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
public class PropertyDictionary : ISuspendable
{
private Dictionary<string, object> _data = new Dictionary<string, object>();
private PropertiesChangedEventArgs _eventArgs;
private bool _modified;
private volatile byte _suspendCount;
private object _syncRoot;
public event EventHandler<PropertiesChangedEventArgs> PropertiesChanged;
protected PropertyDictionary()
{
this._syncRoot = this._data;
this._modified = false;
}
internal virtual void Clear()
{
this._data = new Dictionary<string, object>();
this._syncRoot = this._data;
this._modified = false;
}
internal virtual void CommitProposedProperties(AsyncBizOperation op)
{
}
private static object ForceGetDictionaryValue(IDictionary<string, object> dict, string key)
{
if (!dict.ContainsKey(key))
{
return null;
}
return dict.get_Item(key);
}
public void ForceUpdate()
{
lock (this.SyncRoot)
{
this._suspendCount = 0;
this.InnerFirePropertyChanged();
}
}
public virtual bool HasProposedProperty()
{
lock (this.SyncRoot)
{
Dictionary<string, object>.ValueCollection.Enumerator enumerator = this._data.get_Values().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object obj2 = enumerator.get_Current();
if ((obj2 is IDataCanProposed) && ((IDataCanProposed) obj2).HasProposedValue)
{
return true;
}
}
}
finally
{
enumerator.Dispose();
}
return false;
}
}
protected void InnerFirePropertyChanged()
{
if ((this._suspendCount <= 0) && (this.InnerEventArgs.Count > 0))
{
this.OnPropertiesChanged(this._eventArgs);
this._eventArgs = null;
}
}
protected void InnerFirePropertyChanged<T>(string propertyName, ChangedValuePair<T> context)
{
this.InnerEventArgs.AddChangedProperty<T>(propertyName, context);
this.InnerFirePropertyChanged();
}
protected T InnerGetPropertyValue<T>(string propertyName)
{
lock (this.SyncRoot)
{
if (this._data.ContainsKey(propertyName))
{
return (T) this._data.get_Item(propertyName);
}
T defaultValue = ReflectionHelper.GetDefaultValue<T>();
this._data.Add(propertyName, defaultValue);
return defaultValue;
}
}
protected T InnerGetPropertyValue<T>(string propertyName, T defaultValue)
{
lock (this.SyncRoot)
{
if (this._data.ContainsKey(propertyName))
{
return (T) this._data.get_Item(propertyName);
}
this._data.Add(propertyName, defaultValue);
return defaultValue;
}
}
internal void InnerResetProposedProperties()
{
lock (this.SyncRoot)
{
Dictionary<string, object>.ValueCollection.Enumerator enumerator = this._data.get_Values().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object obj2 = enumerator.get_Current();
if (obj2 is IDataCanProposed)
{
((IDataCanProposed) obj2).RemoveProposedValue();
}
}
}
finally
{
enumerator.Dispose();
}
}
}
protected void InnerSetPropertyValue<T>(string propertyName, T propertyValue)
{
this.InnerSetPropertyValue<T>(propertyName, propertyValue, false);
}
protected void InnerSetPropertyValue<T>(string propertyName, T propertyValue, bool withContext)
{
ChangedValuePair<T> context = null;
lock (this.SyncRoot)
{
T oldVal;
if (this._data.ContainsKey(propertyName))
{
if (object.Equals(this._data.get_Item(propertyName), propertyValue))
{
return;
}
oldVal = (T) this._data.get_Item(propertyName);
this._data.set_Item(propertyName, propertyValue);
}
else
{
oldVal = ReflectionHelper.GetDefaultValue<T>();
this._data.Add(propertyName, propertyValue);
}
if (withContext)
{
context = new ChangedValuePair<T>(oldVal, propertyValue);
}
this._modified = true;
}
this.InnerFirePropertyChanged<T>(propertyName, context);
}
protected virtual void OnPropertiesChanged(PropertiesChangedEventArgs e)
{
FuncDispatcher.InvokeEventHandlerInUiThread<PropertiesChangedEventArgs>(this, this.PropertiesChanged, e);
}
public void ResumeUpdate()
{
lock (this.SyncRoot)
{
if (this._suspendCount > 0)
{
this._suspendCount = (byte) (this._suspendCount - 1);
}
this.InnerFirePropertyChanged();
}
}
public void SuspendUpdate()
{
lock (this.SyncRoot)
{
this._suspendCount = (byte) (this._suspendCount + 1);
}
}
protected void TryRemove(string propertyName)
{
if (this._data.ContainsKey(propertyName))
{
this._data.Remove(propertyName);
}
}
protected IDictionary<string, object> InnerData
{
get
{
return this._data;
}
}
protected PropertiesChangedEventArgs InnerEventArgs
{
get
{
if (this._eventArgs == null)
{
this._eventArgs = new PropertiesChangedEventArgs();
}
return this._eventArgs;
}
}
public object this[string propertyName]
{
get
{
lock (this.SyncRoot)
{
return ForceGetDictionaryValue(this._data, propertyName);
}
}
}
public bool Modified
{
get
{
return this._modified;
}
set
{
this._modified = value;
}
}
public object SyncRoot
{
get
{
return this._syncRoot;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -