SetMemberValue() публичный Метод

public SetMemberValue ( ObjectManager manager, MemberInfo member, object value ) : void
manager ObjectManager
member MemberInfo
value object
Результат void
Пример #1
0
        public void SetMemberValue(ObjectManager manager, MemberInfo member, object value)
        {
            if (member is FieldInfo)
            {
                ((FieldInfo)member).SetValue(ObjectInstance, value);
            }
            else if (member is PropertyInfo)
            {
                ((PropertyInfo)member).SetValue(ObjectInstance, value, null);
            }
            else
            {
                throw new SerializationException("Cannot perform fixup");
            }

            if (Member != null)
            {
                ObjectRecord containerRecord = manager.GetObjectRecord(IdOfContainingObj);
                if (containerRecord.IsRegistered)
                {
                    containerRecord.SetMemberValue(manager, Member, ObjectInstance);
                }
            }
            else if (ArrayIndex != null)
            {
                ObjectRecord containerRecord = manager.GetObjectRecord(IdOfContainingObj);
                if (containerRecord.IsRegistered)
                {
                    containerRecord.SetArrayValue(manager, ObjectInstance, ArrayIndex);
                }
            }
        }
Пример #2
0
        public bool LoadData(ObjectManager manager, ISurrogateSelector selector, StreamingContext context)
        {
            if (Info != null)
            {
                if (Surrogate != null)
                {
                    object new_obj = Surrogate.SetObjectData(ObjectInstance, Info, context, SurrogateSelector);
                    if (new_obj != null)
                    {
                        ObjectInstance = new_obj;
                    }
                    Status = ObjectRecordStatus.ReferenceSolved;
                }
                else if (ObjectInstance is ISerializable)
                {
                    object[]        pars = new object[] { Info, context };
                    ConstructorInfo con  = ObjectInstance.GetType().GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
                    if (con == null)
                    {
                        throw new SerializationException("The constructor to deserialize an object of type " + ObjectInstance.GetType().FullName + " was not found.");
                    }
                    con.Invoke(ObjectInstance, pars);
                }
                else
                {
                    throw new SerializationException("No surrogate selector was found for type " + ObjectInstance.GetType().FullName);
                }

                Info = null;
            }

            if (ObjectInstance is IObjectReference && Status != ObjectRecordStatus.ReferenceSolved)
            {
                try {
                    ObjectInstance = ((IObjectReference)ObjectInstance).GetRealObject(context);
                    int n = 100;
                    while (ObjectInstance is IObjectReference && n > 0)
                    {
                        object ob = ((IObjectReference)ObjectInstance).GetRealObject(context);
                        if (ob == ObjectInstance)
                        {
                            break;
                        }
                        ObjectInstance = ob;
                        n--;
                    }
                    if (n == 0)
                    {
                        throw new SerializationException("The implementation of the IObjectReference interface returns too many nested references to other objects that implement IObjectReference.");
                    }

                    Status = ObjectRecordStatus.ReferenceSolved;
                }
                catch (NullReferenceException) {
                    // Give a second chance
                    return(false);
                }
            }

            if (Member != null)
            {
                // If this object is a value object embedded in another object, the parent
                // object must be updated

                ObjectRecord containerRecord = manager.GetObjectRecord(IdOfContainingObj);
                containerRecord.SetMemberValue(manager, Member, ObjectInstance);
            }
            else if (ArrayIndex != null)
            {
                ObjectRecord containerRecord = manager.GetObjectRecord(IdOfContainingObj);
                containerRecord.SetArrayValue(manager, ObjectInstance, ArrayIndex);
            }

            return(true);
        }
Пример #3
0
 public bool LoadData(ObjectManager manager, ISurrogateSelector selector, StreamingContext context)
 {
     if (this.Info != null)
     {
         if (this.Surrogate != null)
         {
             object obj = this.Surrogate.SetObjectData(this.ObjectInstance, this.Info, context, this.SurrogateSelector);
             if (obj != null)
             {
                 this.ObjectInstance = obj;
             }
             this.Status = ObjectRecordStatus.ReferenceSolved;
         }
         else
         {
             if (!(this.ObjectInstance is ISerializable))
             {
                 throw new SerializationException("No surrogate selector was found for type " + this.ObjectInstance.GetType().FullName);
             }
             object[] parameters = new object[]
             {
                 this.Info,
                 context
             };
             ConstructorInfo constructor = this.ObjectInstance.GetType().GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[]
             {
                 typeof(SerializationInfo),
                 typeof(StreamingContext)
             }, null);
             if (constructor == null)
             {
                 throw new SerializationException("The constructor to deserialize an object of type " + this.ObjectInstance.GetType().FullName + " was not found.");
             }
             constructor.Invoke(this.ObjectInstance, parameters);
         }
         this.Info = null;
     }
     if (this.ObjectInstance is IObjectReference && this.Status != ObjectRecordStatus.ReferenceSolved)
     {
         try
         {
             this.ObjectInstance = ((IObjectReference)this.ObjectInstance).GetRealObject(context);
             int num = 100;
             while (this.ObjectInstance is IObjectReference && num > 0)
             {
                 object realObject = ((IObjectReference)this.ObjectInstance).GetRealObject(context);
                 if (realObject == this.ObjectInstance)
                 {
                     break;
                 }
                 this.ObjectInstance = realObject;
                 num--;
             }
             if (num == 0)
             {
                 throw new SerializationException("The implementation of the IObjectReference interface returns too many nested references to other objects that implement IObjectReference.");
             }
             this.Status = ObjectRecordStatus.ReferenceSolved;
         }
         catch (NullReferenceException)
         {
             return(false);
         }
     }
     if (this.Member != null)
     {
         ObjectRecord objectRecord = manager.GetObjectRecord(this.IdOfContainingObj);
         objectRecord.SetMemberValue(manager, this.Member, this.ObjectInstance);
     }
     else if (this.ArrayIndex != null)
     {
         ObjectRecord objectRecord2 = manager.GetObjectRecord(this.IdOfContainingObj);
         objectRecord2.SetArrayValue(manager, this.ObjectInstance, this.ArrayIndex);
     }
     return(true);
 }