public void Test6() { var t = CTypeData.GetTypeData(typeof(C6)); Assert.AreEqual(typeof(C6), t.Type, "Type incorrect"); Assert.AreEqual(typeof(C5), t.BaseType.Type, "Base Type incorrect"); Assert.AreEqual(2, t.FieldCount, "Field Count"); var f = t[0]; Assert.AreEqual("Top", f.XmlName, "first field"); f = t[1]; Assert.AreEqual("To_2", f.XmlName, "second field"); f = t["Top"]; Assert.AreEqual(typeof(int), f.Field.FieldType, "'Top' field type-wrong"); f = t["To_2"]; Assert.AreEqual(typeof(string), f.Field.FieldType, "'To_2' field type-wrong"); f = t["RenameMe"]; Assert.IsNull(f); f = t["NoSerializeTop"]; Assert.IsNull(f); }
/// <summary> /// Apply any surrogates to the object for serialization /// </summary> /// <param name="_object">The object that may be serialized with a surrogate</param> /// <param name="_elementForObject"> /// The XmlElement that is to receive the object data /// </param> /// <param name="_useType"> /// The Type that the object should be treated as. If NULL, use object.GetType() /// </param> /// <returns> /// TRUE if the serialziation was completed by a surrogate, FALSE if the framework /// should do its job. /// </returns> private bool ApplySurrogates(object _object, XmlElement _elementForObject, Type _useType) { // If nothing is sent in, then do nothing. if (_object == null) { return(false); } // Duplicate References are a "special" kind of built-in surrogate- If this object // has been serialized before, then simply refer to that version and return true. if (HandleDuplicateReferences(_object, _elementForObject)) { return(true); } // Setup var oType = _useType ?? _object.GetType(); var isComplete = false; // Check if the object is a dynamic proxy created by Entity Framework if (CEntityTypeData.UsesEntitySemantics(oType)) { // There are important conventions and differences when dealing with Entities SerializeUsingEntitySemantics(_object, oType, _elementForObject); return(true); } // Check the external surrogate var externalSurrogate = GetExternalSurrogate(oType); if (externalSurrogate != null) { isComplete = externalSurrogate.Serialize(_object, _useType, _elementForObject, this); } if (isComplete) { return(true); } // Check implicit surrogates var typeData = CTypeData.GetTypeData(oType); var impSurrogate = typeData.ImplicitSurrogate; if (impSurrogate != null) { isComplete = impSurrogate.Serialize(_object, _elementForObject, this); } return(isComplete); }
/// <summary> /// Check to see if there are surrogates that handle the deserialization. /// </summary> /// <param name="_xml">The XML containing the object data</param> /// <param name="_type">The TYPE that we're trying to deserialize</param> /// <param name="_workingObject"> /// A "Working Object" that deserializers use to determine if they need to create a new /// object or if they have an object that they can deserialize into. /// </param> /// <returns> /// TRUE if a surrogate was successful in completing the deserialization of the object, /// or FALSE if the surrogates were not able to complete all deserialization /// </returns> private bool ApplySurrogates(XmlElement _xml, Type _type, CWorkingObject _workingObject) { var isComplete = false; _workingObject.WorkingType = _type; // Check for EntitySemantics as a special and overriding Surrogate method The // UseEntitySemantics on the XML element is ignored, as the destination Type gets to // define how it gets deserialized (the attribute may be little more than a Hint to // what happened at serializatiion time) See for more information on EntitySemantics if (CEntityTypeData.UsesEntitySemantics(_type)) { DeserializeUsingEntitySemantics(_xml, _type, _workingObject); return(true); } // Check for external surrogates var externalSurrogate = GetExternalSurrogate(_type); if (externalSurrogate != null) { isComplete = externalSurrogate.Deserialize(_workingObject, _xml, this); } if (isComplete) { return(true); } // Check the Implicit surrogates if the External surrogate(s) didn't finish the // deserialization var typeData = CTypeData.GetTypeData(_type); var implicitSurrogate = typeData.ImplicitSurrogate; if (implicitSurrogate != null && implicitSurrogate.HasSurrogate) { isComplete = implicitSurrogate.Deserialize(_workingObject, _xml, this); } if (isComplete) { return(true); } return(false); }
public void Test5() { var t = CTypeData.GetTypeData(typeof(C6)); t = t.BaseType; Assert.AreEqual(typeof(C5), t.Type, "Type incorrect"); Assert.AreEqual(typeof(C4), t.BaseType.Type, "Base Type incorrect"); Assert.AreEqual(1, t.FieldCount, "Field Count"); var f = t[0]; Assert.AreEqual("Middle", f.XmlName, "first field"); f = t["Middle"]; Assert.AreEqual(typeof(string), f.Field.FieldType, "'Middle' field type-wrong"); f = t["NoSerializeMiddle"]; Assert.IsNull(f); }
public void Test4() { var t = CTypeData.GetTypeData(typeof(C6)); t = t.BaseType; t = t.BaseType; Assert.AreEqual(typeof(C4), t.Type, "Type incorrect"); Assert.AreEqual(typeof(C3), t.BaseType.Type, "Base Type incorrect"); Assert.AreEqual(3, t.FieldCount, "Field Count"); var f = t[0]; Assert.AreEqual("PI_double", f.XmlName, "first field"); f = t[1]; Assert.AreEqual("PIE_string", f.XmlName, "second field"); f = t[2]; Assert.AreEqual("EAT_PIE", f.XmlName, "third field"); f = t["PI_double"]; Assert.AreEqual(typeof(double), f.Field.FieldType, "'PI_double' field type-wrong"); f = t["PIE_string"]; Assert.AreEqual(typeof(string), f.Field.FieldType, "'PIE_string' field type-wrong"); f = t["EAT_PIE"]; Assert.AreEqual(typeof(bool), f.Field.FieldType, "'EAT_PIE' field type-wrong"); f = t["PI"]; Assert.IsNull(f); f = t["PIE"]; Assert.IsNull(f); f = t["something"]; Assert.IsNull(f); }
/// <summary> /// Add all of the fields of a reference type to the XML. Add all of the base classes of /// the type as well. /// </summary> /// <remarks> /// Assume a non-null object. This is a private, thus controlled access, method. /// </remarks> /// <param name="_object"> /// The object to be serialized. We know that the object is a ref-type that may have /// fields and a base-class /// </param> /// <param name="_elementForObject">The Element to add data to for this object</param> private void AddReferenceTypeToXml(object _object, XmlElement _elementForObject) { var oType = _object.GetType(); var typeData = CTypeData.GetTypeData(oType); while (typeData != null) // Need to deal with CTypeData for this and all base-classes of this { AddTypeFieldsToXml(_object, _elementForObject, typeData); ClearIgnoredFields(); var baseTypeData = typeData.BaseType; if (baseTypeData != null) { // Surrogates were already handled for the first iteration of the loop if (ApplySurrogates(_object, _elementForObject, baseTypeData.Type)) { return; } } typeData = baseTypeData; } }
public void TestExplicitConstructor() { var t = CTypeData.GetTypeData(typeof(CExplicitConstructor)); object s = t.ExplicitSurrogate; Assert.IsNotNull(s, "Expecting there to be an explicit constructor on class CExplicitConstructor"); t = CTypeData.GetTypeData(typeof(C6)); s = t.ExplicitSurrogate; Assert.IsNull(s, "Expected there to be no explicit constructor on class C6"); var d = new CDeserializer(); var doc = new XmlDocument(); doc.LoadXml("<_ />"); object o = d.Deserialize <CExplicitConstructor>(doc); Assert.AreEqual <Type>(typeof(CExplicitConstructor), o.GetType()); Assert.AreEqual <bool>(true, (o as CExplicitConstructor).WasSerialized); }
/// <summary> /// The Type is a reference type with zero or more fields, and it is to be deserialized /// using the Framework's algorithm /// </summary> /// <param name="_xml">The XML containing the field data</param> /// <param name="_type">The Type that is to be deserialized</param> /// <param name="_workingObject"> /// The working object to be used if it is set (create new object instance if not) /// </param> /// <returns>The newly deserialized object</returns> private void DeserializeReferenceType(XmlElement _xml, Type _type, CWorkingObject _workingObject) { var typeData = CTypeData.GetTypeData(_type); if (!_workingObject.IsSet) { // Check to see if we can do anything with an explicit constructor (likely to be // private) var o = typeData.TryExplicitConstructor(); if (o == null) { o = Activator.CreateInstance(_type); } if (o == null) { throw new XDeserializationError("Could not create a new object of type " + _type.ToString()); } _workingObject.Set(o); } var currentObject = _workingObject.WorkingObject; while (typeData != null) { DeserializeObjectFields(_xml, currentObject, typeData); ClearIgnoredFields(); typeData = typeData.BaseType; if (typeData != null) { if (ApplySurrogates(_xml, typeData.Type, _workingObject)) { typeData = null; } } } }
public void Test2() { var t = CTypeData.GetTypeData(typeof(C6)); t = t.BaseType; t = t.BaseType; t = t.BaseType; t = t.BaseType; Assert.AreEqual(typeof(C2), t.Type, "Type incorrect"); Assert.AreEqual(typeof(C1), t.BaseType.Type, "Base Type incorrect"); Assert.AreEqual(0, t.FieldCount, "Field Count"); var f = t[0]; Assert.IsNull(f); f = t["ShouldntSerialize"]; Assert.IsNull(f); f = t["AlsoNoSerialization"]; Assert.IsNull(f); }
public void Test1() { var t = CTypeData.GetTypeData(typeof(C6)); t = t.BaseType; t = t.BaseType; t = t.BaseType; t = t.BaseType; t = t.BaseType; Assert.AreEqual(typeof(C1), t.Type, "Type incorrect"); Assert.IsNull(t.BaseType, "Base Type is not null"); Assert.AreEqual(1, t.FieldCount, "Field Count"); var f = t[0]; Assert.AreEqual("ReallyAtBase", f.XmlName, "first field"); f = t["ReallyAtBase"]; Assert.AreEqual(typeof(int), f.Field.FieldType, "'ReallyAtBase' field type-wrong"); f = t["Top"]; Assert.IsNull(f); }
/// <summary> /// Add all of the fields of a given object identified by a CTypeData-descriptor to the /// XML. /// </summary> /// <param name="_object">The object containing the fields / fieldData</param> /// <param name="_elementForObject"> /// The XML Element to add the data to (each field becomes a childNode) /// </param> /// <param name="_typeData"> /// The descriptor which identifies which fields on the _object to add to the XML /// </param> private void AddTypeFieldsToXml(object _object, XmlElement _elementForObject, CTypeData _typeData) { foreach (var fieldData in _typeData) { var fi = fieldData.Field; if (IsIgnoredField(fi.Name)) { continue; } var name = GetFieldName(fieldData); var expectedType = fi.FieldType; var obj = fi.GetValue(_object); PushCurrentField(fieldData); FrameworkSerialize(name, obj, _elementForObject, expectedType); PopField(); } }
/// <summary> /// This routine will enumerate all fields associated with an object and deserialize /// each field. This routine's scope is simply the declared fields for the "top" level /// of the class's inheritance chain- in other words, this will NOT deserialize a base /// class's fields. /// </summary> /// <param name="_xml">The XML containing the field data</param> /// <param name="_objectToPopulate">The object whose fields are being populated</param> /// <param name="_typeData"> /// Serialization Type Data describing this Type (not to include base-class) /// </param> private void DeserializeObjectFields(XmlElement _xml, object _objectToPopulate, CTypeData _typeData) { foreach (var fieldData in _typeData) { var fi = fieldData.Field; if (IsIgnoredField(fi.Name)) { continue; } var expectedType = fi.FieldType; var name = GetFieldName(fieldData); var fixedName = FixMemberName(name); var elem = _xml[fixedName]; if (elem != null) { PushCurrentField(fieldData); var fieldValue = FrameworkDeserialize(elem, expectedType); fi.SetValue(_objectToPopulate, fieldValue); PopField(); } } }
public void Test3() { var t = CTypeData.GetTypeData(typeof(C6)); t = t.BaseType; t = t.BaseType; t = t.BaseType; Assert.AreEqual(typeof(C3), t.Type, "Type incorrect"); Assert.AreEqual(typeof(C2), t.BaseType.Type, "Base Type incorrect"); Assert.AreEqual(3, t.FieldCount, "Field Count"); var f = t[0]; Assert.AreEqual("thisone", f.XmlName, "first field"); f = t[1]; Assert.AreEqual("thatone", f.XmlName, "second field"); f = t[2]; Assert.AreEqual("Same", f.XmlName, "second field"); f = t[-1]; Assert.IsNull(f, "Expected an index outside of 0-2 to return null"); f = t[3]; Assert.IsNull(f, "Expected an index outside of 0-2 to return null"); f = t["thisone"]; Assert.AreEqual(typeof(int), f.Field.FieldType, "'thisone' field type-wrong"); f = t["thatone"]; Assert.AreEqual(typeof(string), f.Field.FieldType, "'thatone' field type-wrong"); f = t["Same"]; Assert.AreEqual(typeof(bool), f.Field.FieldType, "'Same' field type-wrong"); f = t["THISONE"]; Assert.IsNull(f); f = t["THATONE"]; Assert.IsNull(f); f = t["same"]; Assert.IsNull(f); CTestClassesFieldRenamer_Dynamic.UpperCase = true; f = t[0]; Assert.AreEqual("THISONE", f.XmlName, "first field"); f = t[1]; Assert.AreEqual("THATONE", f.XmlName, "second field"); f = t[2]; Assert.AreEqual("Same", f.XmlName, "second field"); f = t["THISONE"]; Assert.AreEqual(typeof(int), f.Field.FieldType, "'THISONE' field type-wrong"); f = t["THATONE"]; Assert.AreEqual(typeof(string), f.Field.FieldType, "'THATONE' field type-wrong"); f = t["Same"]; Assert.AreEqual(typeof(bool), f.Field.FieldType, "'Same' field type-wrong"); f = t["thisone"]; Assert.IsNull(f); f = t["thatone"]; Assert.IsNull(f); f = t["SAME"]; Assert.IsNull(f); }