static class TypeFromBase4
public interface IValue { int Value { get; } }
struct T0 : IValue { public int Value => 0; }
struct T1 : IValue { public int Value => 1; }
struct T2 : IValue { public int Value => 2; }
struct T3 : IValue { public int Value => 3; }
public int Value => default(B).Value * symbols + default(C).Value;
public static int Value(Type t)
return ((IValue)Activator.CreateInstance(t)).Value;
public static int Value(Type t) { return t.GetHashCode(); }
public static Type Create(int num)
for (digitsNum = 1; num >= Math.Pow(symbols, digitsNum); digitsNum++) ;
Span<byte> digits = stackalloc byte[digitsNum];
var digit = (byte)(num % symbols);
digits[digitsNum] = digit;
var currentType = typeof(T0);
foreach (var digit in digits)
var digitType = digit switch
_ => throw new ArgumentOutOfRangeException()
if (currentType == typeof(T0))
currentType = typeof(A<,>).MakeGenericType(currentType, digitType);
public static void Main()
Console.WriteLine(TypeFromBase4.Create(42));
Console.WriteLine(TypeFromBase4.Create(43));