/// <summary> /// Reads XML from the configuration file. /// </summary> /// <param name="stream">The stream.</param> public void ReadFrom(Stream stream) { var serializer = new NaiveSerializer <ValuesContainer>(RuntimeCommon.DefaultLogger); while (stream.Position != stream.Length) { var container = serializer.Deserialize(stream); var category = container.Category; Dictionary <string, ConfigurableValue> existing; if (!categories.TryGetValue(category, out existing)) { categories.Add(category, (existing = new Dictionary <string, ConfigurableValue>())); } foreach (var val in container.Values) { if (existing.ContainsKey(val.Key)) { existing[val.Key] = val; } else { existing.Add(val.Key, val); } } } }
public static ISerializer GetOrCreate <T>() where T : notnull { if (_cache.TryGetValue(typeof(T), out ISerializer? serializer)) { return(serializer); } //if (TypeHelper.IsInternalOrAnonymous<T>()) { serializer = new NaiveSerializer <T>(); //} else { // ImplEmitter<ISerializer> implEmitter = new ImplEmitter<ISerializer>($"S{typeof(T).GUID.ToString("N")}"); // implEmitter.ImplementAction<char, StringBuilder>("SerializeHeader", gen => DefineSerializeHeader<T>(gen)); // implEmitter.ImplementAction<IFormatProvider, char, StringBuilder, object>("SerializeItem", gen => DefineSerializeItem<T>(gen)); // serializer = implEmitter.CreateInstance(); //} _cache.TryAdd(typeof(T), serializer); return(serializer); }
/// <summary> /// Writes the outer tags of this configuration element to the configuration file when implemented in a derived class. /// </summary> /// <returns> /// true if writing was successful; otherwise, false. /// </returns> public void WriteTo(Stream target) { try { var serializer = new NaiveSerializer <ValuesContainer>(RuntimeCommon.DefaultLogger); foreach (var container in Categories.Select(category => new ValuesContainer { Category = category.Key, Values = category.Value.Values.ToArray() })) { serializer.Serialize(container, target); } } catch (Exception e) { Trace.WriteLine(e); } }