/// <summary> /// Registers complex type /// </summary> /// <param name="t"></param> /// <param name="isArray"></param> /// <param name="isEnumerable"></param> /// <param name="isEnum"></param> /// <returns>registered type name</returns> protected internal string RegisterComplexType(Type t, bool isArray, bool isEnumerable, bool isEnum) { string registeredName = "any"; ExtendsAttribute extendsAttr = null; TypeNameAttribute typeNameAttr = null; var typeName = isEnum ? t.Name : string.Format("I{0}", t.Name); typeNameAttr = t.GetCustomAttributes(typeof(TypeNameAttribute), false).OfType <TypeNameAttribute>().FirstOrDefault(); if (typeNameAttr != null) { typeName = typeNameAttr.Name; } if (!isEnum) { extendsAttr = t.GetCustomAttributes(typeof(ExtendsAttribute), false).OfType <ExtendsAttribute>().FirstOrDefault(); StringBuilder extendsSb = null; if (extendsAttr != null && extendsAttr.InterfaceNames.Length > 0) { extendsSb = new StringBuilder("extends "); var isFirst = true; foreach (var intfName in extendsAttr.InterfaceNames) { if (!isFirst) { extendsSb.Append(", "); } extendsSb.Append(intfName); isFirst = false; } } registeredName = GetTypeInterface(t, typeName, extendsSb == null ? null : extendsSb.ToString()); } else { registeredName = GetTSEnum(t, typeName); } if (isArray || isEnumerable) { return(string.Format("{0}[]", typeName)); } return(typeName); }
private static Dictionary <string, Type> GetRegisterTypes(IServiceProvider provider) { IAssemblyFinder assemblyFinder = provider.GetRequiredService <IAssemblyFinder>(); IEnumerable <Type> registerTypes = ReflectionHelper.GetDerivedTypes(assemblyFinder, typeof(ICacheRegister)); Dictionary <string, Type> result = new Dictionary <string, Type>(); foreach (Type registerType in registerTypes) { TypeNameAttribute attr = ReflectionHelper.GetTypeName(registerType); if (attr != null) { result.Add(attr.Name, registerType); } } return(result); }
/// <summary> /// Sets a object-valued setting. /// </summary> /// <param name="Key">Key name.</param> /// <param name="Value">New value.</param> /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns> public static async Task <bool> SetAsync(string Key, object Value) { using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key)) { Setting Setting = await Database.FindFirstDeleteRest <Setting>(new FilterFieldEqualTo("Key", Key)); if (Value is null) { if (!(Setting is ObjectSetting)) { await Database.Delete(Setting); Setting = null; } } else { if (Value is string s) { if (!(Setting is StringSetting StringSetting)) { await Database.Delete(Setting); StringSetting = null; } return(await SetAsyncLocked(Key, s, StringSetting)); } else if (Value is long l) { if (!(Setting is Int64Setting Int64Setting)) { await Database.Delete(Setting); Int64Setting = null; } return(await SetAsyncLocked(Key, l, Int64Setting)); } else if (Value is double d) { if (!(Setting is DoubleSetting DoubleSetting)) { await Database.Delete(Setting); DoubleSetting = null; } return(await SetAsyncLocked(Key, d, DoubleSetting)); } else if (Value is bool b) { if (!(Setting is BooleanSetting BooleanSetting)) { await Database.Delete(Setting); BooleanSetting = null; } return(await SetAsyncLocked(Key, b, BooleanSetting)); } else if (Value is DateTime TP) { if (!(Setting is DateTimeSetting DateTimeSetting)) { await Database.Delete(Setting); DateTimeSetting = null; } return(await SetAsyncLocked(Key, TP, DateTimeSetting)); } else if (Value is TimeSpan TS) { if (!(Setting is TimeSpanSetting TimeSpanSetting)) { await Database.Delete(Setting); TimeSpanSetting = null; } return(await SetAsyncLocked(Key, TS, TimeSpanSetting)); } else { Type T = Value.GetType(); TypeInfo TI = T.GetTypeInfo(); if (!(TI.GetCustomAttribute(typeof(CollectionNameAttribute)) is null)) { throw new InvalidOperationException("Object setting values cannot be stored separate collections. (CollectionName attribute found.)"); } TypeNameAttribute TypeNameAttribute = TI.GetCustomAttribute <TypeNameAttribute>(); if (TypeNameAttribute.TypeNameSerialization != TypeNameSerialization.FullName) { throw new InvalidOperationException("Full Type names must be serialized when persisting object setting values. (TypeName attribute.). Exceptions for the types: Boolean, Int64, String, DateTime, TimeSpan, Double."); } } } if (Setting is ObjectSetting ObjectSetting) { if (((ObjectSetting.Value is null) ^ (Value is null)) || !ObjectSetting.Value.Equals(Value)) { ObjectSetting.Value = Value; await Database.Update(ObjectSetting); return(true); } else { return(false); } }