Exemplo n.º 1
0
        public object Load(TextReader reader)
        {
            var ctx = new SerializationReaderContext(reader.ReadToEnd());
            var obj = Read(ctx);

            return(obj);
        }
Exemplo n.º 2
0
        string ParseNodeType(SerializationReaderContext ctx, int from)
        {
            int    indexOfNodeNickStart = ctx.Xaml.IndexOf('<', from) + 1;
            int    indexOfNodeNickEnd   = ctx.Xaml.IndexOf(' ', indexOfNodeNickStart);
            string nodeType             = ctx.Xaml.Substring(indexOfNodeNickStart, indexOfNodeNickEnd - indexOfNodeNickStart);

            return(nodeType);
        }
        /*
         * object ReadNodeObject(SerializationReaderContext ctx, int from)
         * {
         *      var nodeAttribs = ReadAttributes(ctx, from);
         *      if (!ctx.NamespaceToAlias.Any())
         *      {
         *              ReadSystemAttributes(ctx, nodeAttribs);
         *      }
         *      string nodeType = ParseNodeType(ctx, from);
         *      var type = ctx.GetType(nodeType);
         *      var obj = Activator.CreateInstance(type);
         *
         *      foreach (var attrib in nodeAttribs)
         *      {
         *              type.GetProperty(attrib.Key).SetValue(obj, attrib.Value);
         *      }
         *
         *      return obj;
         * }
         *
         * void ReadSystemAttributes(SerializationReaderContext ctx, IDictionary<string, string> attribs)
         * {
         *      foreach (var kvp in attribs.ToArray())
         *      {
         *              if (kvp.Key == "xmlns")
         *              {
         *                      ctx.RootNamespace = kvp.Value;
         *                      attribs.Remove(kvp);
         *              }
         *              if (kvp.Key.StartsWith("xmlns:"))
         *              {
         *                      var i = kvp.Key.IndexOf(':');
         *                      var pref = kvp.Key.Substring(i + 1);
         *                      ctx.NamespaceToAlias[kvp.Value] = pref;
         *                      ctx.AliasToNamespace[pref] = kvp.Value;
         *                      attribs.Remove(kvp);
         *              }
         *      }
         * }
         *
         * string ParseNodeType(SerializationReaderContext ctx, int from)
         * {
         *      int indexOfNodeNickStart = ctx.Xaml.IndexOf('<', from) + 1;
         *      int indexOfNodeNickEnd = ctx.Xaml.IndexOf(' ', indexOfNodeNickStart);
         *      string nodeType = ctx.Xaml.Substring(indexOfNodeNickStart, indexOfNodeNickEnd - indexOfNodeNickStart);
         *      return nodeType;
         * }
         *
         * IDictionary<string, string> ReadAttributes(SerializationReaderContext ctx, int from)
         * {
         *      var dic = new Dictionary<string, string>();
         *      int indexOfNodeEnd = ctx.Xaml.IndexOf('>', from);
         *      int i = from;
         *      do
         *      {
         *              i = ctx.Xaml.IndexOf(' ', i + 1);
         *              int iProNameEnd = ctx.Xaml.IndexOf('=', i);
         *              if (iProNameEnd < 0)
         *              {
         *                      break;
         *              }
         *              string proName = ctx.Xaml.Substring(i, iProNameEnd - i).Trim();
         *              int iProValStart = ctx.Xaml.IndexOf('"', i) + 1;
         *              int iProValEnd = ctx.Xaml.IndexOf('"', iProValStart + 1);
         *              string proVal = ctx.Xaml.Substring(iProValStart, iProValEnd - iProValStart);
         *              dic[proName] = proVal;
         *      } while (i < indexOfNodeEnd && i >= 0);
         *      return dic;
         * }
         */

        object UniversalReader(SerializationReaderContext ctx, Type type)
        {
            var obj = Activator.CreateInstance(type);

            foreach (var attrib in ctx.CurrentNodeAttribs)
            {
                var pi = type.GetProperty(attrib.Key);
                if (pi == null)
                {
                    throw new Exception($"Property {attrib.Key} not found");
                }
                pi.SetValue(obj, ConvertFromString(pi.PropertyType, attrib.Value));
            }

            return(obj);
        }
Exemplo n.º 4
0
        object ReadNodeObject(SerializationReaderContext ctx, int from)
        {
            var nodeAttribs = ReadAttributes(ctx, from);

            if (!ctx.NamespaceToAlias.Any())
            {
                ReadSystemAttributes(ctx, nodeAttribs);
            }
            string nodeType = ParseNodeType(ctx, from);
            var    type     = ctx.GetType(nodeType);

            ctx.CurrentNodeAttribs = nodeAttribs;

            var obj = ObjectReaderFor(type)(ctx);

            return(obj);
        }
Exemplo n.º 5
0
 void ReadSystemAttributes(SerializationReaderContext ctx, IDictionary <string, string> attribs)
 {
     foreach (var kvp in attribs.ToArray())
     {
         if (kvp.Key == "xmlns")
         {
             ctx.RootNamespace = kvp.Value;
             attribs.Remove(kvp);
         }
         if (kvp.Key.StartsWith("xmlns:"))
         {
             var i    = kvp.Key.IndexOf(':');
             var pref = kvp.Key.Substring(i + 1);
             ctx.NamespaceToAlias[kvp.Value] = pref;
             ctx.AliasToNamespace[pref]      = kvp.Value;
             attribs.Remove(kvp);
         }
     }
 }
Exemplo n.º 6
0
        IDictionary <string, string> ReadAttributes(SerializationReaderContext ctx, int from)
        {
            var dic            = new Dictionary <string, string>();
            int indexOfNodeEnd = ctx.Xaml.IndexOf('>', from);
            int i = ctx.Xaml.IndexOf(' ', from + 1);;

            do
            {
                int iProNameEnd = ctx.Xaml.IndexOf('=', i);
                if (iProNameEnd < 0)
                {
                    break;
                }
                string proName      = ctx.Xaml.Substring(i, iProNameEnd - i).Trim();
                int    iProValStart = ctx.Xaml.IndexOf('"', i) + 1;
                int    iProValEnd   = ctx.Xaml.IndexOf('"', iProValStart + 1);
                string proVal       = ctx.Xaml.Substring(iProValStart, iProValEnd - iProValStart);
                dic[proName] = proVal;
                i            = ctx.Xaml.IndexOf(' ', i + 1);
            } while (i < indexOfNodeEnd && i >= 0);
            return(dic);
        }
 private object PrecompiledReader(SerializationReaderContext arg)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 8
0
 object Read(SerializationReaderContext ctx)
 {
     return(ReadNodeObject(ctx, 0));
 }