Mercurial > pub > ImplabNet
comparison Implab.ServiceHost/Unity/ConfigurationSchema.cs @ 269:ff581cff7003 v3
Working on Unity container xml configuration
| author | cin |
|---|---|
| date | Tue, 24 Apr 2018 01:46:02 +0300 |
| parents | |
| children | d4d437ec4483 |
comparison
equal
deleted
inserted
replaced
| 268:0be8a6ae8307 | 269:ff581cff7003 |
|---|---|
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 using System.Reflection; | |
| 4 using System.Xml; | |
| 5 using System.Xml.Serialization; | |
| 6 using Implab.Components; | |
| 7 | |
| 8 namespace Implab.ServiceHost.Unity { | |
| 9 public class ConfigurationSchema { | |
| 10 | |
| 11 public static ConfigurationSchema Default { get; private set; } = CreateDefault(); | |
| 12 | |
| 13 readonly Dictionary<Tuple<string,string>, LazyAndWeak<XmlSerializer>> m_mappings = new Dictionary<Tuple<string, string>, LazyAndWeak<XmlSerializer>>(); | |
| 14 | |
| 15 public void DefineMapping(string name, string ns, Type type) { | |
| 16 Safe.ArgumentNotEmpty(name, nameof(name)); | |
| 17 Safe.ArgumentNotNull(type, nameof(type)); | |
| 18 ns = ns ?? string.Empty; | |
| 19 m_mappings[Tuple.Create(name, ns)] = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(type), true); | |
| 20 } | |
| 21 | |
| 22 public void DefineMapping<T>() { | |
| 23 var xmlRoot = typeof(T).GetCustomAttribute<XmlRootAttribute>(); | |
| 24 var ns = xmlRoot?.Namespace; | |
| 25 var root = xmlRoot?.ElementName ?? typeof(T).Name; | |
| 26 DefineMapping(root, ns, typeof(T)); | |
| 27 } | |
| 28 | |
| 29 public T Deserialize<T>(XmlReader reader) { | |
| 30 reader.MoveToContent(); | |
| 31 var name = reader.Name; | |
| 32 var ns = reader.NamespaceURI; | |
| 33 | |
| 34 return (T)m_mappings[Tuple.Create(name, ns)].Value.Deserialize(reader); | |
| 35 } | |
| 36 | |
| 37 static ConfigurationSchema CreateDefault() { | |
| 38 var schema = new ConfigurationSchema(); | |
| 39 | |
| 40 schema.DefineMapping<RegisterElement>(); | |
| 41 | |
| 42 return schema; | |
| 43 } | |
| 44 | |
| 45 | |
| 46 } | |
| 47 } |
