BITKit/Packages/Core/Property/Property.cs

173 lines
4.5 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SqlServer.Server;
using System.IO;
namespace BITKit
{
/// <summary>
/// 属性接口,用于序列化引用
/// </summary>
public interface IProperty
{
}
/// <summary>
/// 属性接口
/// GetOrCreate(√)
/// GetOrAdd(√)
/// TryGet(√)
/// TryRemove(√)
/// TrySet(√)
/// GetProperties(√)
/// </summary>
public interface IPropertable: IBinarySerialize
{
/// <summary>
/// 是否拥有属性
/// </summary>
bool Contains<T>();
/// <summary>
/// 获取或创建属性
/// </summary>
T GetOrCreateProperty<T>();
/// <summary>
/// 获取或通过工厂方法添加属性
/// </summary>
T GetOrAddProperty<T>(Func<T> addFactory);
/// <summary>
/// 尝试获取属性
/// </summary>
bool TryGetProperty<T>(out T value);
/// <summary>
/// 尝试移除属性
/// </summary>
bool TryRemoveProperty<T>();
/// <summary>
/// 尝试设置属性
/// </summary>
bool TrySetProperty<T>(T value);
/// <summary>
/// 获取所有属性
/// </summary>
object[] GetProperties();
/// <summary>
/// 清除所有属性
/// </summary>
/// <returns></returns>
bool ClearProperties();
/// <summary>
/// 从其他对象复制属性
/// </summary>
/// <param name="propertable"></param>
/// <returns></returns>
bool CopyPropertiesFrom(IPropertable propertable);
}
public class Property : IPropertable
{
public Property() { }
public Property(IEnumerable<object> factory)
{
foreach (var x in factory)
{
properties.Add(x.GetType().FullName, x);
}
}
Dictionary<string, object> properties=new();
public T GetProperty<T>()
{
return (T)properties[typeof(T).FullName];
}
public void SetProperty<T>(T value)
{
properties.Insert(typeof(T).FullName, value);
}
public bool Contains<T>()
{
return properties.ContainsKey(typeof(T).FullName);
}
public T GetOrCreateProperty<T>()
{
return GetOrAddProperty(Activator.CreateInstance<T>);
}
public T GetOrAddProperty<T>(Func<T> addFactory)
{
if (properties.TryGetValue(typeof(T).FullName, out var x))
{
return (T)x;
}
else
{
properties.Add(typeof(T).FullName, x = addFactory.Invoke());
return (T)x;
}
}
public bool TryGetProperty<T>(out T value)
{
if (properties.TryGetValue(typeof(T).FullName, out var x))
{
value = (T)x;
return true;
}
else
{
value=default(T);
return false;
}
}
public bool TryRemoveProperty<T>()
{
if(properties.TryGetValue(typeof(T).FullName, out var x))
{
properties.Remove(typeof(T).Name);
return true;
}
return false;
}
public bool TrySetProperty<T>(T value)
{
if (properties.TryGetValue(typeof(T).FullName, out var x))
{
properties[typeof(T).FullName] = value;
return true;
}
else
{
return false;
}
}
public object[] GetProperties()=>properties.Values.ToArray();
public void Read(BinaryReader r)
{
foreach (var x in properties)
{
}
}
public void Write(BinaryWriter w)
{
throw new NotImplementedException();
}
public bool ClearProperties()
{
properties.Clear();
return true;
}
public bool CopyPropertiesFrom(IPropertable propertable)
{
ClearProperties();
foreach (var x in propertable.GetProperties())
{
properties.Add(x.GetType().FullName, x);
}
return true;
}
}
}