public void WriteEnum <TEnum, TDotNetType>(TEnum val) where TEnum : struct { var enumType = typeof(TEnum); if (!enumType.IsEnum) { throw new ArgumentException("TEnum must be an enum."); } if (!IsSteamCompatibleNumericalType(typeof(TDotNetType))) { throw new InvalidOperationException("TDotNetType must correspond to a valid Steam numerical type."); } Action <BufferBuilder, object> writer; if (!DotNetTypeWriteMethods.TryGetValue(typeof(TDotNetType), out writer)) { throw new ArgumentException($"Unable to write {typeof(TDotNetType).Name}"); } var enumCompatibleValue = Convert.ChangeType(val, typeof(TDotNetType)); writer(this, enumCompatibleValue); }
private static bool IsSteamCompatibleNumericalType(Type t) { return(DotNetTypeWriteMethods.ContainsKey(t) && t != typeof(string)); }