/// <summary>
 /// Initializes a new instance of the <see cref="DataMemberAccessor"/> class using the
 /// getter and setter from an exisiting instance but with a new name and default value.
 /// </summary>
 /// <param name="dma">The existing DataMemberAccessor.</param>
 /// <param name="name">The new name.</param>
 /// <param name="defaultValue">The new default value.</param>
 public DataMemberAccessor(DataMemberAccessor dma, string name, object defaultValue)
 {
     Get               = dma.Get;
     Set               = dma.Set;
     this.Name         = name;
     this.DefaultValue = defaultValue;
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataMemberAccessor"/> class using the
 /// getter and setter from an exisiting instance but with a new name and default value.
 /// </summary>
 /// <param name="dma">The existing DataMemberAccessor.</param>
 /// <param name="name">The new name.</param>
 /// <param name="defaultValue">The new default value.</param>
 public DataMemberAccessor(DataMemberAccessor dma, string name, object defaultValue) {
   Get = dma.Get;
   Set = dma.Set;
   this.Name = name;
   this.DefaultValue = defaultValue;
 }
Esempio n. 3
0
        private IEnumerator <ISerializationToken> Serialize(DataMemberAccessor accessor, object obj)
        {
            object value = accessor.Get(obj);

            if (value == null)
            {
                return(NullReferenceEnumerator(accessor.Name));
            }
            Type type = value.GetType();

            if (obj2id.ContainsKey(value))
            {
                return(ReferenceEnumerator(accessor.Name, obj2id[value]));
            }
            bool emitTypeInfo = false;

            if (!typeCache.ContainsKey(type))
            {
                typeCache.Add(type, typeCache.Count);
                emitTypeInfo = InterleaveTypeInformation;
            }
            int typeId = typeCache[type];
            int?id     = null;

            if (!type.IsValueType)
            {
                id = obj2id.Count;
                obj2id.Add(value, (int)id);
            }
            try {
                objectGraphTrace.Push(accessor.Name);
                IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type);
                if (primitiveSerializer != null)
                {
                    return(PrimitiveEnumerator(
                               accessor.Name,
                               typeId,
                               primitiveSerializer.Format(value),
                               id,
                               emitTypeInfo));
                }
                ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type);
                if (compositeSerializer != null)
                {
                    return(CompositeEnumerator(
                               accessor.Name,
                               compositeSerializer.Decompose(value),
                               id,
                               typeId,
                               compositeSerializer.CreateMetaInfo(value),
                               emitTypeInfo));
                }
                throw CreatePersistenceException(type, "Could not determine how to serialize a value.", true);
            }
            catch (Exception x) {
                if (isTestRun)
                {
                    exceptions.Add(x);
                    return(new List <ISerializationToken>().GetEnumerator());
                }
                else if (x is PersistenceException)
                {
                    throw;
                }
                else
                {
                    throw CreatePersistenceException(
                              type,
                              string.Format("Uncaught exception during serialization:{0}{1}", Environment.NewLine, x),
                              false);
                }
            }
            finally {
                objectGraphTrace.Pop();
            }
        }