This commit is contained in:
CortexCore
2025-01-09 17:49:38 +08:00
parent 0287c1a2fc
commit 01e7e4e35e
5 changed files with 159 additions and 16 deletions

View File

@@ -23,33 +23,35 @@ namespace BITKit
public static string GetDisplayName(this object self)
{
{
if (self.GetType().GetCustomAttribute<DisplayNameAttribute>() is { } displayNameAttribute)
var type = self is Type t? t: self.GetType();
if (type.GetCustomAttribute<DisplayNameAttribute>() is { } displayNameAttribute)
{
return displayNameAttribute.DisplayName;
}
#if NET5_0_OR_GREATER
if (self.GetType().GetCustomAttribute<DisplayAttribute>() is { } displayAttribute)
if (type.GetCustomAttribute<DisplayAttribute>() is { } displayAttribute)
{
return displayAttribute.Name;
}
#endif
}
{
if (self is Enum)
{
var field = self.GetType().GetField(self.ToString()!);
if (self is not Enum) return self.ToString();
var field = self.GetType().GetField(self.ToString()!);
if(field is null) return self.ToString();
if (field.GetCustomAttribute<DisplayNameAttribute>() is DisplayNameAttribute displayNameAttribute)
{
return displayNameAttribute.DisplayName;
}
#if NET5_0_OR_GREATER
if (field.GetCustomAttribute<DisplayAttribute>() is DisplayAttribute displayAttribute)
{
return displayAttribute.Name;
}
#endif
if (field.GetCustomAttribute<DisplayNameAttribute>() is { } displayNameAttribute)
{
return displayNameAttribute.DisplayName;
}
#if NET5_0_OR_GREATER
if (field.GetCustomAttribute<DisplayAttribute>() is { } displayAttribute)
{
return displayAttribute.Name;
}
#endif
}
return self.ToString();
}

View File

@@ -66,7 +66,7 @@ namespace BITKit
public override string ToString()
{
return JsonConvert.SerializeObject(this);
return JsonConvert.SerializeObject(this,Formatting.Indented);
}
[JsonProperty("status_code")] public int StatusCode = 200;

View File

@@ -16,6 +16,34 @@ namespace BITKit
public T Value { get; set; }=default;
public object Obj { get => Value; set => Value = (T)value; }
}
public class PropertyWrapper<T> : IWrapper<T>
{
private readonly PropertyInfo _field;
private readonly object _target;
public event Action<T, T> OnValueChanged;
public PropertyWrapper(PropertyInfo field, object target)
{
_field = field;
_target = target ?? throw new ArgumentNullException(nameof(target));
}
public T Value
{
get => (T)_field.GetValue(_target);
set
{
var prev = Value;
_field.SetValue(_target, value);
OnValueChanged?.Invoke(prev, value);
}
}
public object Obj
{
get => _field.GetValue(_target);
set => _field.SetValue(_target, value);
}
}
public class FieldWrapper<T> : IWrapper<T>
{
private readonly FieldInfo _field;