public static CachedSharedUser GetOrAddSharedUser(this IGatewayDispatcher dispatcher, ISynchronizedDictionary <Snowflake, CachedSharedUser> userCache, UserJsonModel model) { return(userCache.GetOrAdd(model.Id, static (_, tuple) => { var(client, model) = tuple; return new CachedSharedUser(client, model); }, (dispatcher.Client, model))); }
private JsonConverter GetConverter(Type type) { if (typeof(Stream).IsAssignableFrom(type)) { return(_streamConverter); } if (typeof(IJsonNode).IsAssignableFrom(type) && !typeof(JsonModel).IsAssignableFrom(type)) { return(_jsonNodeConverter); } if (!type.IsClass) { var nullableType = Nullable.GetUnderlyingType(type); if (nullableType != null) { type = nullableType; } if (type.IsEnum) { var stringEnumAttribute = type.GetCustomAttribute <StringEnumAttribute>(); if (stringEnumAttribute != null) { return(_stringEnumConverter); } } else if (type == typeof(Snowflake)) { return(_snowflakeConverter); } } else { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { var generics = type.GetGenericArguments(); if (generics[0] == typeof(Snowflake)) { return(_snowflakeDictionaryConverters.GetOrAdd(generics[1], type => Activator.CreateInstance(typeof(SnowflakeDictionaryConverter <>).MakeGenericType(type)) as JsonConverter)); } } } return(null); }
private OptionalConverter GetOptionalConverter(Type type) { var optionalType = type.GenericTypeArguments[0]; return(_optionalConverters.GetOrAdd(optionalType, (x, @this) => OptionalConverter.Create(@this.GetConverter(x)), this)); }
static ISynchronizedDictionary <IEmoji, Button> GetButtons(MenuBase menu) { var methods = _typeCache.GetOrAdd(menu.GetType(), static x => { var methods = x.GetMethods(BindingFlags.Public | BindingFlags.Instance); IDictionary <IEmoji, MethodInfo> buttons = null; for (var i = 0; i < methods.Length; i++) { var method = methods[i]; var buttonAttribute = method.GetCustomAttribute <ButtonAttribute>(); if (buttonAttribute == null) { continue; } if (method.ContainsGenericParameters) { throw new InvalidOperationException("A button callback must not contain generic parameters."); } if (method.ReturnType != typeof(ValueTask)) { throw new InvalidOperationException("A button callback must return a non-generic ValueTask."); } var parameters = method.GetParameters(); if (parameters.Length != 1 || parameters[0].ParameterType != typeof(ButtonEventArgs)) { throw new InvalidOperationException("A button callback must contain a single ButtonEventArgs parameter."); } if (buttons == null) { buttons = new Dictionary <IEmoji, MethodInfo>(); } buttons.Add(buttonAttribute.Emoji, method); } KeyValuePair <IEmoji, MethodInfo>[] array; if (buttons != null) { array = new KeyValuePair <IEmoji, MethodInfo> [buttons.Count]; buttons.CopyTo(array, 0); } else { array = Array.Empty <KeyValuePair <IEmoji, MethodInfo> >(); } return(array); }); var buttons = new SynchronizedDictionary <IEmoji, Button>(methods.Length); for (var i = 0; i < methods.Length; i++) { var(emoji, method) = methods[i]; var button = ButtonFactory(emoji, method, i, menu); buttons.Add(button.Emoji, button); } return(buttons); }