using System; using System.Diagnostics; namespace Babushka.scripts.CSharp.Common.Util; public class Variant { private Type _type; private Object _value; public Type GetVariantType() { return _type; } public T GetValue() { if (_type != typeof(T)) throw new ArgumentOutOfRangeException( $"The Variant does not store a {typeof(T)}. The current type is {_type}"); return (T)_value; } public void SetValue(T value) { if (typeof(T1) != typeof(T) && typeof(T2) != typeof(T)) throw new ArgumentOutOfRangeException( $"The Variant does not support type {typeof(T)}. Supported types are {typeof(T1)} and {typeof(T2)}"); _type = typeof(T); _value = value!; } public static implicit operator T1(Variant v) { return v.GetValue(); } public static implicit operator T2(Variant v) { return v.GetValue(); } public static implicit operator Variant(T1 t) { return new Variant { _type = typeof(T1), _value = t! }; } public static implicit operator Variant(T2 t) { return new Variant { _type = typeof(T2), _value = t! }; } public bool IsType() { if (typeof(T1) != typeof(T) && typeof(T2) != typeof(T)) throw new ArgumentOutOfRangeException( $"The Variant does not support type {typeof(T)}. Supported types are {typeof(T1)} and {typeof(T2)}"); return _type == typeof(T); } }