You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.2 KiB
126 lines
3.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using static Apewer.TextUtility;
|
|
|
|
#if !NET20
|
|
using System.Dynamic;
|
|
#endif
|
|
|
|
namespace Apewer
|
|
{
|
|
|
|
/// <summary>对象字典模型。</summary>
|
|
[Serializable]
|
|
public class ObjectSet : ObjectSet<object>
|
|
{
|
|
|
|
/// <summary></summary>
|
|
public ObjectSet(bool trimKey = false) : base(trimKey) { }
|
|
|
|
}
|
|
|
|
/// <summary>字典模型。</summary>
|
|
[Serializable]
|
|
public partial class ObjectSet<T> : IToJson
|
|
{
|
|
|
|
private Dictionary<string, T> _origin;
|
|
|
|
private bool _trimkey = false;
|
|
|
|
private bool _locked = false;
|
|
|
|
/// <summary>获取或设置字典内容。</summary>
|
|
public T this[string key] { get { return Get(key); } set { Set(key, value); } }
|
|
|
|
/// <summary>获取字典。</summary>
|
|
internal Dictionary<string, T> Origin { get { return _origin; } }
|
|
|
|
internal bool Locked { get { return _locked; } set { _locked = value; } }
|
|
|
|
internal bool TrimKey { get { return _trimkey; } set { _trimkey = value; } }
|
|
|
|
/// <summary>构造函数。</summary>
|
|
public ObjectSet(bool trimKey = false)
|
|
{
|
|
_trimkey = trimKey;
|
|
_origin = new Dictionary<string, T>();
|
|
}
|
|
|
|
private T Get(string key)
|
|
{
|
|
var contains = false;
|
|
return Get(key, ref contains);
|
|
}
|
|
|
|
private T Get(string key, ref bool contains)
|
|
{
|
|
var value = default(T);
|
|
if (key != null)
|
|
{
|
|
var k = _trimkey ? Trim(key) : key;
|
|
lock (_origin)
|
|
{
|
|
contains = _origin.ContainsKey(k);
|
|
if (contains) value = _origin[k];
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
private bool Set(string key, T value)
|
|
{
|
|
if (_locked) return false;
|
|
var success = false;
|
|
if (key != null)
|
|
{
|
|
var k = _trimkey ? Trim(key) : key;
|
|
lock (_origin)
|
|
{
|
|
if (_origin.ContainsKey(k)) _origin[k] = value;
|
|
else _origin.Add(k, value);
|
|
success = true;
|
|
}
|
|
}
|
|
return success;
|
|
}
|
|
|
|
/// <summary>转换为 <see cref="Json"/> 对象。</summary>
|
|
public Json ToJson() => Json.From(_origin);
|
|
|
|
/// <summary>转换为 <see cref="Json"/> 字符串。</summary>
|
|
public override string ToString()
|
|
{
|
|
var json = ToJson();
|
|
if (json == null) return null;
|
|
return json.ToString(true);
|
|
}
|
|
|
|
}
|
|
|
|
#if !NET20
|
|
|
|
/// <summary>字典模型。</summary>
|
|
public partial class ObjectSet<T> : DynamicObject
|
|
{
|
|
|
|
/// <summary></summary>
|
|
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
|
{
|
|
var contains = false;
|
|
result = Get(binder.Name, ref contains);
|
|
return contains;
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public override bool TrySetMember(SetMemberBinder binder, object value)
|
|
{
|
|
return Set(binder.Name, (T)value);
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|