示例#1
0
 public object Deserialize(IStorage storage, Data.Node data, object result)
 {
     if (result.IsNull())
         try
         {
             result = data.Type.Create();
         }
         catch (System.MissingMethodException e)
         {
             if (data.Type == data.OriginalType)
                 new Exception.CreateAbstract(e, data.Type, data.Region).Throw();
             else
                 new Exception.UnknownType(e, data.OriginalType, data.Region).Throw();
         }
     if (result.NotNull())
     {
         Reflect.Property[] properties = result.GetProperties();
         if (data is Data.Branch)
             foreach (Data.Node node in (data as Data.Branch).Nodes)
             {
                 string name = node.Name.Convert(storage.Casing, Casing.Pascal);
                 Reflect.Property property = properties.Find(p =>
                 {
                     Kean.Serialize.ParameterAttribute[] attributes;
                     return (attributes = p.GetAttributes<Kean.Serialize.ParameterAttribute>()).Length > 0 && attributes[0].Name.NotEmpty() ? attributes[0].Name == node.Name : p.Name == name;
                 });
                 if (property.IsNull())
                     new Exception.PropertyMissing(data.Type, name, node.Region).Throw();
                 else if (property.Writable)
                     storage.Deserialize(node, property.Type, d => property.Data = d);
                 else if (property.Readable && (property.Type.Category == Reflect.TypeCategory.Class || property.Type.Category == Reflect.TypeCategory.Array || property.Type.Category == Reflect.TypeCategory.Interface))
                     storage.DeserializeContent(node.DefaultType(property.Type), property.Data);
                 else
                     new Exception.PropertyNotWriteable(data.Type, name, node.Region).Throw();
             }
     }
     return result;
 }