Better implementation of CustomProperties

This commit is contained in:
Michael Becker 2016-01-27 12:50:56 -05:00
parent f930759d0a
commit 648ff27997
2 changed files with 21 additions and 77 deletions

View File

@ -66,7 +66,26 @@ namespace UniversalEditor
}
}
private ObjectModelCustomProperty.ObjectModelCustomPropertyCollection mvarCustomProperties = new ObjectModelCustomProperty.ObjectModelCustomPropertyCollection();
public ObjectModelCustomProperty.ObjectModelCustomPropertyCollection CustomProperties { get { return mvarCustomProperties; } }
private Dictionary<string, object> _customProperties = new Dictionary<string, object>();
public T GetCustomProperty<T>(string name, T defaultValue = default(T))
{
if (_customProperties.ContainsKey(name))
{
return (T)_customProperties[name];
}
return defaultValue;
}
public object GetCustomProperty(string name, object defaultValue = null)
{
return GetCustomProperty<object>(name, defaultValue);
}
public void SetCustomProperty<T>(string name, T value)
{
_customProperties[name] = value;
}
public void SetCustomProperty(string name, object value)
{
SetCustomProperty<object>(name, value);
}
}
}

View File

@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor
{
public class ObjectModelCustomProperty
{
public class ObjectModelCustomPropertyCollection
{
private Dictionary<DataFormatReference, Dictionary<string, ObjectModelCustomProperty>> _internalCollection = new Dictionary<DataFormatReference, Dictionary<string, ObjectModelCustomProperty>>();
public ObjectModelCustomProperty Add(DataFormatReference dataFormat, string name, object value)
{
ObjectModelCustomProperty item = new ObjectModelCustomProperty();
item.DataFormat = dataFormat;
item.Name = name;
item.Value = value;
Dictionary<string, ObjectModelCustomProperty> values = null;
if (!_internalCollection.ContainsKey(dataFormat))
{
values = new Dictionary<string, ObjectModelCustomProperty>();
_internalCollection.Add(dataFormat, values);
}
else
{
values = _internalCollection[dataFormat];
}
values[name] = item;
return item;
}
public ObjectModelCustomProperty[] this[DataFormatReference dataFormat]
{
get
{
List<ObjectModelCustomProperty> list = new List<ObjectModelCustomProperty>();
if (_internalCollection.ContainsKey(dataFormat))
{
foreach (KeyValuePair<string, ObjectModelCustomProperty> kvp in _internalCollection[dataFormat])
{
list.Add(kvp.Value);
}
}
return list.ToArray();
}
}
public ObjectModelCustomProperty this[DataFormatReference dataFormat, string name]
{
get
{
if (!_internalCollection.ContainsKey(dataFormat)) return null;
if (!_internalCollection[dataFormat].ContainsKey(name)) return null;
return _internalCollection[dataFormat][name];
}
set
{
if (!_internalCollection.ContainsKey(dataFormat)) _internalCollection.Add(dataFormat, new Dictionary<string,ObjectModelCustomProperty>());
if (!_internalCollection[dataFormat].ContainsKey(name)) _internalCollection[dataFormat].Add(name, value);
_internalCollection[dataFormat][name] = value;
}
}
}
private DataFormatReference mvarDataFormat = null;
public DataFormatReference DataFormat { get { return mvarDataFormat; } set { mvarDataFormat = value; } }
private string mvarName = String.Empty;
public string Name { get { return mvarName; } set { mvarName = value; } }
private object mvarValue = null;
public object Value { get { return mvarValue; } set { mvarValue = value; } }
}
}