public static MemberInfo[] GetSerializableMembers(Type type) { return(RuntimeFormatterServices.GetSerializableMembers(type)); }
public static object GetUninitializedObject(Type type) { return(RuntimeFormatterServices.GetUninitializedObject(type)); }
public static Type GetTypeFromAssembly(Assembly assembly, string typeName) { return(RuntimeFormatterServices.GetTypeFromAssembly(assembly, typeName)); }
internal void CompleteObject(ObjectHolder holder, bool bObjectFullyComplete) { FixupHolderList fixupHolderList = holder.m_missingElements; object member = (object)null; ObjectHolder holder1 = (ObjectHolder)null; int num = 0; if (holder.ObjectValue == null) { throw new SerializationException(Environment.GetResourceString("Serialization_MissingObject", (object)holder.m_id)); } if (fixupHolderList == null) { return; } if (holder.HasSurrogate || holder.HasISerializable) { SerializationInfo serializationInfo1 = holder.m_serInfo; if (serializationInfo1 == null) { throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFixupDiscovered")); } if (fixupHolderList != null) { for (int index = 0; index < fixupHolderList.m_count; ++index) { if (fixupHolderList.m_values[index] != null && this.GetCompletionInfo(fixupHolderList.m_values[index], out holder1, out member, bObjectFullyComplete)) { object objectValue = holder1.ObjectValue; if (this.CanCallGetType(objectValue)) { SerializationInfo serializationInfo2 = serializationInfo1; string name = (string)member; object obj = objectValue; Type type = obj.GetType(); serializationInfo2.UpdateValue(name, obj, type); } else { serializationInfo1.UpdateValue((string)member, objectValue, typeof(MarshalByRefObject)); } ++num; fixupHolderList.m_values[index] = (FixupHolder)null; if (!bObjectFullyComplete) { holder.DecrementFixupsRemaining(this); holder1.RemoveDependency(holder.m_id); } } } } } else { for (int index = 0; index < fixupHolderList.m_count; ++index) { FixupHolder fixup = fixupHolderList.m_values[index]; if (fixup != null && this.GetCompletionInfo(fixup, out holder1, out member, bObjectFullyComplete)) { if (holder1.TypeLoadExceptionReachable) { holder.TypeLoadException = holder1.TypeLoadException; if (holder.Reachable) { throw new SerializationException(Environment.GetResourceString("Serialization_TypeLoadFailure", (object)holder.TypeLoadException.TypeName)); } } if (holder.Reachable) { holder1.Reachable = true; } switch (fixup.m_fixupType) { case 1: if (holder.RequiresValueTypeFixup) { throw new SerializationException(Environment.GetResourceString("Serialization_ValueTypeFixup")); } ((Array)holder.ObjectValue).SetValue(holder1.ObjectValue, (int[])member); break; case 2: MemberInfo fi = (MemberInfo)member; if (fi.MemberType != MemberTypes.Field) { throw new SerializationException(Environment.GetResourceString("Serialization_UnableToFixup")); } if (holder.RequiresValueTypeFixup && holder.ValueTypeFixupPerformed) { if (!this.DoValueTypeFixup((FieldInfo)fi, holder, holder1.ObjectValue)) { throw new SerializationException(Environment.GetResourceString("Serialization_PartialValueTypeFixup")); } } else { FormatterServices.SerializationSetValue(fi, holder.ObjectValue, holder1.ObjectValue); } if (holder1.RequiresValueTypeFixup) { holder1.ValueTypeFixupPerformed = true; break; } break; default: throw new SerializationException(Environment.GetResourceString("Serialization_UnableToFixup")); } ++num; fixupHolderList.m_values[index] = (FixupHolder)null; if (!bObjectFullyComplete) { holder.DecrementFixupsRemaining(this); holder1.RemoveDependency(holder.m_id); } } } } this.m_fixupCount = this.m_fixupCount - (long)num; if (fixupHolderList.m_count != num) { return; } holder.m_missingElements = (FixupHolderList)null; }
public static object PopulateObjectMembers(object obj, MemberInfo[] members, object[] data) { return(RuntimeFormatterServices.PopulateObjectMembers(obj, members, data)); }
private bool DoValueTypeFixup(FieldInfo memberToFix, ObjectHolder holder, object value) { FieldInfo[] sourceArray = new FieldInfo[4]; FieldInfo[] flds = null; int index = 0; int[] indices = null; ValueTypeFixupInfo valueFixup = null; object objectValue = holder.ObjectValue; while (holder.RequiresValueTypeFixup) { if ((index + 1) >= sourceArray.Length) { FieldInfo[] destinationArray = new FieldInfo[sourceArray.Length * 2]; Array.Copy(sourceArray, destinationArray, sourceArray.Length); sourceArray = destinationArray; } valueFixup = holder.ValueFixup; objectValue = holder.ObjectValue; if (valueFixup.ParentField != null) { FieldInfo parentField = valueFixup.ParentField; ObjectHolder holder2 = this.FindObjectHolder(valueFixup.ContainerID); if (holder2.ObjectValue == null) { break; } if (Nullable.GetUnderlyingType(parentField.FieldType) != null) { sourceArray[index] = parentField.FieldType.GetField("value", BindingFlags.NonPublic | BindingFlags.Instance); index++; } sourceArray[index] = parentField; holder = holder2; index++; } else { holder = this.FindObjectHolder(valueFixup.ContainerID); indices = valueFixup.ParentIndex; if (holder.ObjectValue != null) { } break; } } if (!(holder.ObjectValue is Array) && (holder.ObjectValue != null)) { objectValue = holder.ObjectValue; } if (index != 0) { flds = new FieldInfo[index]; for (int i = 0; i < index; i++) { FieldInfo info3 = sourceArray[(index - 1) - i]; SerializationFieldInfo info4 = info3 as SerializationFieldInfo; flds[i] = (info4 == null) ? info3 : info4.FieldInfo; } TypedReference reference = TypedReference.MakeTypedReference(objectValue, flds); if (memberToFix != null) { ((RuntimeFieldInfo)memberToFix).SetValueDirect(reference, value); } else { TypedReference.SetTypedReference(reference, value); } } else if (memberToFix != null) { FormatterServices.SerializationSetValue(memberToFix, objectValue, value); } if ((indices != null) && (holder.ObjectValue != null)) { ((Array)holder.ObjectValue).SetValue(objectValue, indices); } return(true); }
private bool DoValueTypeFixup(FieldInfo memberToFix, ObjectHolder holder, object value) { FieldInfo[] fieldInfoArray1 = new FieldInfo[4]; int length = 0; int[] numArray = (int[])null; object objectValue = holder.ObjectValue; while (holder.RequiresValueTypeFixup) { if (length + 1 >= fieldInfoArray1.Length) { FieldInfo[] fieldInfoArray2 = new FieldInfo[fieldInfoArray1.Length * 2]; Array.Copy((Array)fieldInfoArray1, (Array)fieldInfoArray2, fieldInfoArray1.Length); fieldInfoArray1 = fieldInfoArray2; } ValueTypeFixupInfo valueFixup = holder.ValueFixup; objectValue = holder.ObjectValue; if (valueFixup.ParentField != (FieldInfo)null) { FieldInfo parentField = valueFixup.ParentField; ObjectHolder objectHolder = this.FindObjectHolder(valueFixup.ContainerID); if (objectHolder.ObjectValue != null) { if (Nullable.GetUnderlyingType(parentField.FieldType) != (Type)null) { fieldInfoArray1[length] = parentField.FieldType.GetField("value", BindingFlags.Instance | BindingFlags.NonPublic); ++length; } fieldInfoArray1[length] = parentField; holder = objectHolder; ++length; } else { break; } } else { holder = this.FindObjectHolder(valueFixup.ContainerID); numArray = valueFixup.ParentIndex; if (holder.ObjectValue != null) { break; } break; } } if (!(holder.ObjectValue is Array) && holder.ObjectValue != null) { objectValue = holder.ObjectValue; } if (length != 0) { FieldInfo[] flds = new FieldInfo[length]; for (int index = 0; index < length; ++index) { FieldInfo fieldInfo = fieldInfoArray1[length - 1 - index]; SerializationFieldInfo serializationFieldInfo = fieldInfo as SerializationFieldInfo; flds[index] = (FieldInfo)serializationFieldInfo == (FieldInfo)null ? fieldInfo : (FieldInfo)serializationFieldInfo.FieldInfo; } TypedReference target = TypedReference.MakeTypedReference(objectValue, flds); if (memberToFix != (FieldInfo)null) { memberToFix.SetValueDirect(target, value); } else { TypedReference.SetTypedReference(target, value); } } else if (memberToFix != (FieldInfo)null) { FormatterServices.SerializationSetValue((MemberInfo)memberToFix, objectValue, value); } if (numArray != null && holder.ObjectValue != null) { ((Array)holder.ObjectValue).SetValue(objectValue, numArray); } return(true); }
internal static SerializationInfo PrepareConstructorArgs(object serObj, string[] fieldNames, object[] fieldValues, out StreamingContext context) { SerializationInfo si = null; if (serObj is ISerializable) { si = new SerializationInfo(serObj.GetType(), s_converter); for (int i = 0; i < fieldNames.Length; i++) { if (fieldNames[i] != null) { si.AddValue(fieldNames[i], fieldValues[i]); } } } else { Hashtable fields = new Hashtable(); int incomingFieldIndex = 0; int numIncomingFields = 0; for (; incomingFieldIndex < fieldNames.Length; incomingFieldIndex++) { if (fieldNames[incomingFieldIndex] != null) { fields[fieldNames[incomingFieldIndex]] = fieldValues[incomingFieldIndex]; numIncomingFields++; } } MemberInfo[] mi = FormatterServices.GetSerializableMembers(serObj.GetType()); for (int index = 0; index < mi.Length; index++) { string fieldName = mi[index].Name; if (!fields.Contains(fieldName)) { // If we are missing a field value then it's not necessarily // the end of the world: check whether the field is marked // [OptionalField]. Object [] attrs = mi[index].GetCustomAttributes(typeof(OptionalFieldAttribute), false); if (attrs == null || attrs.Length == 0) { throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Serialization_MissingMember"), mi[index], serObj.GetType(), typeof(OptionalFieldAttribute).FullName)); } continue; } object value = fields[fieldName]; FormatterServices.SerializationSetValue(mi[index], serObj, value); } } context = s_cloneContext; return(si); }
public static MemberInfo[] GetSerializableMembers(Type type) { return(FormatterServices.GetSerializableMembers(type, new StreamingContext(StreamingContextStates.All))); }
internal static object UnsafeGetUninitializedObject(Type type) { return(FormatterServices.GetUninitializedObject(type)); }
// GetRealObject intercepts the deserialization process in order to allow deserializing part of the // object's inheritance heirarchy using standard ISerializable constructors, and the remaining // portion using the saved serialization states. object IObjectReference.GetRealObject(StreamingContext context) { // If we've already deserialized the real object, use that rather than deserializing it again if (m_realObject != null) { return(m_realObject); } // If we don't have a real type to deserialize, then this is really a SafeSerializationManager // and we don't need to rebuild the object that we're standing in for. if (m_realType == null) { return(this); } // Look for the last type in GetRealType's inheritance hierarchy which implements a critical // deserialization constructor. This will be the object that we use as the deserialization // construction type to initialize via standard ISerializable semantics // First build up the chain starting at the type below Object and working to the real type we // serialized. Stack inheritanceChain = new Stack(); RuntimeType currentType = m_realType; do { inheritanceChain.Push(currentType); currentType = currentType.BaseType as RuntimeType; }while (currentType != typeof(object)); // Now look for the first type that does not implement the ISerializable .ctor. When we find // that, previousType will point at the last type that did implement the .ctor. We require that // the .ctor we invoke also be non-transparent RuntimeConstructorInfo serializationCtor = null; RuntimeType previousType = null; do { previousType = currentType; currentType = inheritanceChain.Pop() as RuntimeType; serializationCtor = currentType.GetSerializationCtor(); }while (serializationCtor != null && serializationCtor.IsSecurityCritical); // previousType is the last type that did implement the deserialization .ctor before the first // type that did not, so we'll grab it's .ctor to use for deserialization. BCLDebug.Assert(previousType != null, "We should have at least one inheritance from the base type"); serializationCtor = ObjectManager.GetConstructor(previousType); // Allocate an instance of the final type and run the selected .ctor on that instance to get the // standard ISerializable initialization done. object deserialized = FormatterServices.GetUninitializedObject(m_realType); serializationCtor.SerializationInvoke(deserialized, m_savedSerializationInfo, context); m_savedSerializationInfo = null; m_realType = null; // Save away the real object that was deserialized so that we can fill it in later, and return // it back as the object that should result from the final deserialization. m_realObject = deserialized; return(deserialized); }
void ReadCollection(CollectionDataContract collectionContract) { Type type = collectionContract.UnderlyingType; Type itemType = collectionContract.ItemType; bool isArray = (collectionContract.Kind == CollectionKind.Array); ConstructorInfo constructor = collectionContract.Constructor; if (type.IsInterface) { switch (collectionContract.Kind) { case CollectionKind.GenericDictionary: type = Globals.TypeOfDictionaryGeneric.MakeGenericType(itemType.GetGenericArguments()); constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Globals.EmptyTypeArray, null); break; case CollectionKind.Dictionary: type = Globals.TypeOfHashtable; constructor = XmlFormatGeneratorStatics.HashtableCtor; break; case CollectionKind.Collection: case CollectionKind.GenericCollection: case CollectionKind.Enumerable: case CollectionKind.GenericEnumerable: case CollectionKind.List: case CollectionKind.GenericList: type = itemType.MakeArrayType(); isArray = true; break; } } string itemName = collectionContract.ItemName; string itemNs = collectionContract.StableName.Namespace; if (!isArray) { if (type.IsValueType) { // FIXME: this is not what the original code does. objectLocal = FormatterServices.GetUninitializedObject(type); } else { objectLocal = constructor.Invoke(new object [0]); context.AddNewObject(objectLocal); } } int size = context.GetArraySize(); string objectId = context.GetObjectId(); bool canReadPrimitiveArray = false, readResult = false; if (isArray && TryReadPrimitiveArray(type, itemType, size, out readResult)) { canReadPrimitiveArray = true; } if (!readResult) { if (size == -1) { object growingCollection = null; if (isArray) { growingCollection = Array.CreateInstance(itemType, 32); } int i = 0; // FIXME: I cannot find i++ part, but without that it won't work as expected. for (; i < int.MaxValue; i++) { if (IsStartElement(this.itemName, this.itemNamespace)) { context.IncrementItemCount(1); object value = ReadCollectionItem(collectionContract, itemType, itemName, itemNs); if (isArray) { MethodInfo ensureArraySizeMethod = XmlFormatGeneratorStatics.EnsureArraySizeMethod.MakeGenericMethod(itemType); growingCollection = ensureArraySizeMethod.Invoke(null, new object [] { growingCollection, i }); ((Array)growingCollection).SetValue(value, i); } else { StoreCollectionValue(objectLocal, itemType, value, collectionContract); } } else if (IsEndElement()) { break; } else { HandleUnexpectedItemInCollection(ref i); } } if (isArray) { MethodInfo trimArraySizeMethod = XmlFormatGeneratorStatics.TrimArraySizeMethod.MakeGenericMethod(itemType); objectLocal = trimArraySizeMethod.Invoke(null, new object [] { growingCollection, i }); context.AddNewObjectWithId(objectId, objectLocal); } } else { context.IncrementItemCount(size); if (isArray) { objectLocal = Array.CreateInstance(itemType, size); context.AddNewObject(objectLocal); } // FIXME: I cannot find j++ part, but without that it won't work as expected. for (int j = 0; j < size; j++) { if (IsStartElement(this.itemName, this.itemNamespace)) { var itemValue = ReadCollectionItem(collectionContract, itemType, itemName, itemNs); if (isArray) { ((Array)objectLocal).SetValue(itemValue, j); } else { StoreCollectionValue(objectLocal, itemType, itemValue, collectionContract); } } else { HandleUnexpectedItemInCollection(ref j); } } context.CheckEndOfArray(xmlReader, size, this.itemName, this.itemNamespace); } } if (canReadPrimitiveArray) { context.AddNewObjectWithId(objectId, objectLocal); } }
private object GetUninitializedObject(Type objType) { return(FormatterServices.GetUninitializedObject(objType)); }
internal static object UnsafeGetUninitializedObject(int id) { return(FormatterServices.GetUninitializedObject(DataContract.GetDataContractForInitialization(id).TypeForInitialization)); }
public static MemberInfo[] GetSerializableMembers(Type type, StreamingContext context) { return(RuntimeFormatterServices.GetSerializableMembers(type, context)); }
public static MemberInfo[] GetSerializableMembers(Type type, StreamingContext context) { if (type == null) { throw new ArgumentNullException("type"); } if (!(type is RuntimeType)) { throw new SerializationException(Environment.GetResourceString("Serialization_InvalidType", new object[] { type.ToString() })); } MemberHolder key = new MemberHolder(type, context); return(FormatterServices.m_MemberInfoTable.GetOrAdd(key, (MemberHolder _) => FormatterServices.InternalGetSerializableMembers((RuntimeType)type))); }
public static object[] GetObjectData(object obj, MemberInfo[] memberInfos) { return(RuntimeFormatterServices.GetObjectData(obj, memberInfos)); }
internal void CompleteObject(ObjectHolder holder, bool bObjectFullyComplete) { FixupHolderList missingElements = holder.m_missingElements; object member = null; ObjectHolder holder3 = null; int num = 0; if (holder.ObjectValue == null) { throw new SerializationException(Environment.GetResourceString("Serialization_MissingObject", new object[] { holder.m_id })); } if (missingElements != null) { if (holder.HasSurrogate || holder.HasISerializable) { SerializationInfo serInfo = holder.m_serInfo; if (serInfo == null) { throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFixupDiscovered")); } if (missingElements != null) { for (int i = 0; i < missingElements.m_count; i++) { if ((missingElements.m_values[i] != null) && this.GetCompletionInfo(missingElements.m_values[i], out holder3, out member, bObjectFullyComplete)) { object objectValue = holder3.ObjectValue; if (this.CanCallGetType(objectValue)) { serInfo.UpdateValue((string)member, objectValue, objectValue.GetType()); } else { serInfo.UpdateValue((string)member, objectValue, typeof(MarshalByRefObject)); } num++; missingElements.m_values[i] = null; if (!bObjectFullyComplete) { holder.DecrementFixupsRemaining(this); holder3.RemoveDependency(holder.m_id); } } } } } else { for (int j = 0; j < missingElements.m_count; j++) { MemberInfo info2; FixupHolder fixup = missingElements.m_values[j]; if ((fixup == null) || !this.GetCompletionInfo(fixup, out holder3, out member, bObjectFullyComplete)) { continue; } if (holder3.TypeLoadExceptionReachable) { holder.TypeLoadException = holder3.TypeLoadException; if (holder.Reachable) { throw new SerializationException(Environment.GetResourceString("Serialization_TypeLoadFailure", new object[] { holder.TypeLoadException.TypeName })); } } if (holder.Reachable) { holder3.Reachable = true; } switch (fixup.m_fixupType) { case 1: if (holder.RequiresValueTypeFixup) { throw new SerializationException(Environment.GetResourceString("Serialization_ValueTypeFixup")); } break; case 2: info2 = (MemberInfo)member; if (info2.MemberType != MemberTypes.Field) { throw new SerializationException(Environment.GetResourceString("Serialization_UnableToFixup")); } if (!holder.RequiresValueTypeFixup || !holder.ValueTypeFixupPerformed) { goto Label_0242; } if (!this.DoValueTypeFixup((FieldInfo)info2, holder, holder3.ObjectValue)) { throw new SerializationException(Environment.GetResourceString("Serialization_PartialValueTypeFixup")); } goto Label_0256; default: throw new SerializationException(Environment.GetResourceString("Serialization_UnableToFixup")); } ((Array)holder.ObjectValue).SetValue(holder3.ObjectValue, (int[])member); goto Label_0289; Label_0242: FormatterServices.SerializationSetValue(info2, holder.ObjectValue, holder3.ObjectValue); Label_0256: if (holder3.RequiresValueTypeFixup) { holder3.ValueTypeFixupPerformed = true; } Label_0289: num++; missingElements.m_values[j] = null; if (!bObjectFullyComplete) { holder.DecrementFixupsRemaining(this); holder3.RemoveDependency(holder.m_id); } } } this.m_fixupCount -= num; if (missingElements.m_count == num) { holder.m_missingElements = null; } } }