63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
using System;
|
|
using System.Reflection;
|
|
|
|
namespace BITKit
|
|
{
|
|
public interface IWrapper
|
|
{
|
|
public object Obj { get; set; }
|
|
}
|
|
public interface IWrapper<T>:IWrapper
|
|
{
|
|
public T Value { get; set; }
|
|
}
|
|
public class ValueWrapper<T> : IWrapper<T>
|
|
{
|
|
public T Value { get; set; }=default;
|
|
public object Obj { get => Value; set => Value = (T)value; }
|
|
}
|
|
public class FieldWrapper<T> : IWrapper<T>
|
|
{
|
|
private readonly FieldInfo _field;
|
|
private readonly object _target;
|
|
public FieldWrapper(FieldInfo field, object target)
|
|
{
|
|
_field = field;
|
|
_target = target ?? throw new ArgumentNullException(nameof(target));
|
|
}
|
|
public T Value
|
|
{
|
|
get => (T)_field.GetValue(_target);
|
|
set => _field.SetValue(_target, value);
|
|
}
|
|
|
|
public object Obj
|
|
{
|
|
get => _field.GetValue(_target);
|
|
set => _field.SetValue(_target, value);
|
|
}
|
|
}
|
|
public class FuncWrapper<T> : IWrapper<T>
|
|
{
|
|
private readonly Func<T> _get;
|
|
private readonly Action<T> _set;
|
|
public FuncWrapper(Func<T> get, Action<T> set)
|
|
{
|
|
_get = get;
|
|
_set = set;
|
|
}
|
|
public T Value
|
|
{
|
|
get => _get();
|
|
set => _set(value);
|
|
}
|
|
|
|
public object Obj
|
|
{
|
|
get => _get();
|
|
set => _set((T)value);
|
|
}
|
|
}
|
|
}
|
|
|