BITKit/Src/Core/Utility/References.cs

93 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace BITKit
{
public interface IReference
{
string Get();
string Value => Get();
string Replace(string value) => Get().Replace("{x}",value);
}
public static class IReferenceExtensions
{
public static string[] Cast(this IEnumerable<IReference> self)
{
return self.Select(Get).ToArray();
string Get(IReference x) => x.Value;
}
}
public interface IReference<T>
{
T Get();
}
[System.Serializable]
public abstract record References : IReference
{
public abstract string Get();
public static implicit operator string(References self) => self.Get();
public override string ToString() => Get();
}
[System.Serializable]
public abstract record References<T> : IReference<T>
{
public abstract T Get();
public static implicit operator T(References<T> self) => self.Get();
}
[Serializable]
public struct Reference : IReference
{
public Reference(string value)
{
this.value = value;
}
#if UNITY_EDITOR
[UnityEngine.TextArea]
#endif
public string value;
public string Get() => value;
}
[System.Serializable]
public record Reference<T> : References<T>
{
public T value;
public Reference()
{
}
public Reference(T value)
{
this.value = value;
}
public override T Get() => value;
}
#if NET5_0_OR_GREATER
[Serializable]
public class DataReference<T>:IReference<T>
{
public string Key;
public DataReference(){}
public DataReference(string key)
{
Key = key;
}
public T Get() => Data.Get<T>(Key);
public static implicit operator T(DataReference<T> self)
{
return self.Get();
}
}
#endif
}