BITFALL/Assets/BITKit/Core/Property/Property.cs

178 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace BITKit
{
/// <summary>
/// 属性接口,用于序列化引用
/// </summary>
public interface IProperty
{
}
/// <summary>
/// 属性接口
/// GetOrCreate(√)
/// GetOrAdd(√)
/// TryGet(√)
/// TryRemove(√)
/// TrySet(√)
/// GetProperties(√)
/// </summary>
public interface IPropertable
{
/// <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);
}
}
private readonly List<object> properties=new();
public T GetProperty<T>()
{
return properties.OfType<T>().First();
}
public void SetProperty<T>(T value)
{
for (var i = 0; i < properties.Count; i++)
{
if (properties[i] is T)
properties[i] = value;
}
}
public bool Contains<T>()
{
return properties.OfType<T>().FirstOrDefault() is not null;
}
public T GetOrCreateProperty<T>()
{
return GetOrAddProperty(Activator.CreateInstance<T>);
}
public T GetOrAddProperty<T>(Func<T> addFactory)
{
foreach (var obj in properties)
{
if (obj is T t) return t;
}
T x;
properties.Add(x = addFactory.Invoke());
return x;
}
public bool TryGetProperty<T>(out T value)
{
try
{
value = properties.OfType<T>().First();
return true;
}
catch (InvalidOperationException)
{
value = default;
return false;
}
}
public bool TryRemoveProperty<T>()
{
var removed=false;
foreach (var value in properties.OfType<T>().ToArray())
{
properties.Remove(value);
removed = true;
}
// if(properties.TryGetValue(typeof(T).FullName, out var x))
// {
// properties.Remove(typeof(T).Name);
// return true;
// }
return removed;
}
public bool TrySetProperty<T>(T value)
{
var current = properties.OfType<T>().FirstOrDefault();
if (current is not null)
{
properties.Remove(current);
return true;
}
properties.Add(value);
return false;
}
public object[] GetProperties() => properties.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();
properties.AddRange( propertable.GetProperties());;
return true;
}
}
}