This commit is contained in:
CortexCore
2024-04-16 04:06:46 +08:00
parent 37e7fcea51
commit c798b224be
67 changed files with 1305 additions and 425 deletions

View File

@@ -70,87 +70,84 @@ namespace BITKit
{
foreach (var x in factory)
{
AddPropertyInternal(x);
properties.Add(x);
}
}
Dictionary<string, object> properties=new();
private readonly List<object> properties=new();
public T GetProperty<T>()
{
return (T)properties[typeof(T).FullName];
return properties.OfType<T>().First();
}
public void SetProperty<T>(T value)
{
properties.Insert(typeof(T).FullName, value);
for (var i = 0; i < properties.Count; i++)
{
if (properties[i] is T)
properties[i] = value;
}
}
public bool Contains<T>()
{
return properties.ContainsKey(typeof(T).FullName);
return properties.OfType<T>().FirstOrDefault() is not null;
}
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))
foreach (var obj in properties)
{
return (T)x;
}
else
{
AddPropertyInternal(x =addFactory.Invoke());
//properties.Add(typeof(T).FullName, x = addFactory.Invoke());
return (T)x;
if (obj is T t) return t;
}
T x;
properties.Add(x = addFactory.Invoke());
return x;
}
public bool TryGetProperty<T>(out T value)
{
if (properties.TryGetValue(typeof(T).FullName, out var x))
try
{
value = (T)x;
value = properties.OfType<T>().First();
return true;
}
else
catch (InvalidOperationException)
{
value=default(T);
value = default;
return false;
}
}
public bool TryRemoveProperty<T>()
{
foreach (var pair in properties.Where(x=>x.Value is T))
var removed=false;
foreach (var value in properties.OfType<T>().ToArray())
{
properties.Remove(pair.Key);
return true;
properties.Remove(value);
removed = true;
}
// if(properties.TryGetValue(typeof(T).FullName, out var x))
// {
// properties.Remove(typeof(T).Name);
// return true;
// }
return false;
return removed;
}
public bool TrySetProperty<T>(T value)
{
bool result = false;
foreach (var pair in properties.Where(x=>x.Value is T).ToArray())
var current = properties.OfType<T>().FirstOrDefault();
if (current is not null)
{
properties[pair.Key] = value;
result = true;
properties.Remove(current);
return true;
}
return result;
// if (properties.TryGetValue(typeof(T).FullName, out var x))
// {
// properties[typeof(T).FullName] = value;
// return true;
// }
// else
// {
// return false;
// }
properties.Add(value);
return false;
}
public object[] GetProperties()=>properties.Values.Distinct().ToArray();
public object[] GetProperties() => properties.ToArray();
public void Read(BinaryReader r)
{
@@ -174,23 +171,8 @@ namespace BITKit
public bool CopyPropertiesFrom(IPropertable propertable)
{
ClearProperties();
foreach (var x in propertable.GetProperties())
{
AddPropertyInternal(x);
}
properties.AddRange( propertable.GetProperties());;
return true;
}
private void AddPropertyInternal(object value)
{
if (value is ICloneable cloneable)
{
value = cloneable.Clone();
}
properties.Set(value.GetType().FullName, value);
foreach (var att in value.GetType().GetCustomAttributes<CustomTypeAttribute>())
{
properties.Set(att.Type!.FullName, value);
}
}
}
}