protected byte[] TestSerialize <TClassType>(TClassType objectToSerialize, VerifySerializedBytesDelegate verifyFunction, SerializationDefaults serializationDefaults = null) where TClassType : class, T { // We want to test loading by attributes and by file Serializer.Instance.GetClassInfo(typeof(TClassType), true, serializationDefaults); byte[] serializedBytes = Serializer.Instance.Serialize(objectToSerialize); verifyFunction(serializedBytes, objectToSerialize); if (_includeFileTesting) { ConfigMessageSerializerClass.WriteDefaultToFile(typeof(TClassType)); ConfigMessageSerializerClass configMessageSerializerClass = ConfigMessageSerializerClass.ReadFromFile(typeof(TClassType)); List <ConfigMessageSerializerClass> classList = new List <ConfigMessageSerializerClass>() { configMessageSerializerClass }; Serializer.Instance.GetClassInfo(typeof(TClassType), classList, true, serializationDefaults); byte[] serializedBytesConfig = Serializer.Instance.Serialize(objectToSerialize); verifyFunction(serializedBytesConfig, objectToSerialize); Assert.That(serializedBytes.SequenceEqual(serializedBytesConfig), Is.True, "BytesEqual"); } return(serializedBytes); }
public void TestSimpleXmlTestClass() { string filename = "SimpleXmlTestClass.xml"; XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConfigMessageSerializerClass)); FileStream fileStream = new FileStream(filename, FileMode.Open); ConfigMessageSerializerClass readMessageSerializerClassInfo = (ConfigMessageSerializerClass)xmlSerializer.Deserialize(fileStream); List <ConfigMessageSerializerClass> configMessageSerializerClasses = new List <ConfigMessageSerializerClass>(); configMessageSerializerClasses.Add(readMessageSerializerClassInfo); Serializer.Instance.LoadSerializableClassesFromSettings(configMessageSerializerClasses); SimpleXmlTestClass simpleXmlTestClass = new SimpleXmlTestClass(); simpleXmlTestClass.MessageType = 0x03; simpleXmlTestClass.Value1 = 0x12345678; simpleXmlTestClass.ValueString = "ValueString"; byte[] serializedBytes = Serializer.Instance.Serialize(simpleXmlTestClass); SimpleXmlTestClass deserializedSimpleXmlTestClass = Serializer.Instance.Deserialize <SimpleXmlTestClass>(serializedBytes); Assert.That(simpleXmlTestClass.MessageType, Is.EqualTo(deserializedSimpleXmlTestClass.MessageType)); Assert.That(simpleXmlTestClass.Value1, Is.EqualTo(deserializedSimpleXmlTestClass.Value1)); Assert.That(simpleXmlTestClass.Length, Is.EqualTo(deserializedSimpleXmlTestClass.Length)); Assert.That(simpleXmlTestClass.ValueString, Is.EqualTo(deserializedSimpleXmlTestClass.ValueString)); }
public void DoSimpleTest() { string filename = "SimpleTestClass.xml"; ConfigMessageSerializerClass testMessageSerializerClassInfo = CreateClassInfo(); //XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConfigMessageSerializerClass), new Type[] { typeof(MessagePropertyAttribute), typeof(CalculatedLengthResultAttribute)}); XmlAttributeOverrides attributeOverrides = new XmlAttributeOverrides(); XmlAttributes attributes = new XmlAttributes(); attributes.XmlElements.Add(new XmlElementAttribute(typeof(MessagePropertyAttribute).FullName, typeof(MessagePropertyAttribute))); attributes.XmlElements.Add(new XmlElementAttribute(typeof(CalculatedLengthResultAttribute).FullName, typeof(CalculatedLengthResultAttribute))); attributes.XmlElements.Add(new XmlElementAttribute(typeof(CalculatedAuthenticationResultAttribute).FullName, typeof(CalculatedAuthenticationResultAttribute))); attributeOverrides.Add(typeof(ConfigPropertyInfo), "Attributes", attributes); XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConfigMessageSerializerClass), attributeOverrides); TextWriter writer = new StreamWriter(filename); xmlSerializer.Serialize(writer, testMessageSerializerClassInfo); writer.Close(); // If the XML document has been altered with unknown // nodes or attributes, handles them with the // UnknownNode and UnknownAttribute events. //serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode); //serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute); FileStream fileStream = new FileStream(filename, FileMode.Open); ConfigMessageSerializerClass readMessageSerializerClassInfo = (ConfigMessageSerializerClass)xmlSerializer.Deserialize(fileStream); VerifyClassInfo(readMessageSerializerClassInfo, testMessageSerializerClassInfo); }
// Note: This is run on a separate thread, mostly so the status can be updated while it is being run void ProcessClasses(Object stateInfo) { bool successful = false; do { ClassData classData = (ClassData)stateInfo; if (classData.Classes == null || classData.Classes.Count == 0) { AddStatus("There were no classes specified to process"); break; } try { if (!Directory.Exists(classData.OutputDirectory)) { AddStatus(string.Format("Creating output directory {0}", classData.OutputDirectory)); Directory.CreateDirectory(classData.OutputDirectory); AddStatus(string.Format("Created output directory {0}", classData.OutputDirectory)); } } catch (Exception ex) { AddStatus(string.Format("Failed trying to create output directory {0}: {1}", classData.OutputDirectory, ex)); break; } bool allClassesSuccessful = true; AddStatus(string.Format("Processing {0} classes", classData.Classes.Count)); foreach (Object itemObject in classData.Classes) { Type type = (Type)itemObject; string filename = classData.FilenameFormat; filename = filename.Replace("{ClassName}", type.Name); filename = Path.Combine(classData.OutputDirectory, filename); AddStatus(string.Format("Creating class description for {0} in {1}", type.FullName, filename)); try { ConfigMessageSerializerClass.WriteDefaultToFile(filename, type); AddStatus(string.Format("Created class description for {0} in {1}", type.FullName, filename)); } catch (Exception ex) { AddStatus(string.Format("Failed trying to create file for {0}: {1}", type.FullName, ex)); allClassesSuccessful = false; } } successful = allClassesSuccessful; } while (false); AddStatus("Done processing classes"); ProcessingComplete(successful); }
// Some example code: https://docs.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization ConfigMessageSerializerClass CreateClassInfo() { ConfigMessageSerializerClass messageSerializerClassInfo = new ConfigMessageSerializerClass(); ConfigClassInfo classInfo = new ConfigClassInfo(); classInfo.AssemblyName = Assembly.GetExecutingAssembly().FullName; classInfo.ClassFullName = typeof(SimpleXmlTestClass).FullName; ConfigPropertyInfo propertyInfo1 = new ConfigPropertyInfo(); propertyInfo1.Name = "Property1"; propertyInfo1.Attributes.Add(new MessagePropertyAttribute() { PrepadCharacter = 'T' }); propertyInfo1.Attributes.Add(new CalculatedLengthResultAttribute()); //propertyInfo1._messagePropertyAttribute.MessageLengthType = MessageLengthTypes.RestOfMessage; //propertyInfo1._messagePropertyAttribute.PrepadCharacter = 'T'; //propertyInfo1.AuthenticationClass = typeof(int); classInfo.Properties.Add(propertyInfo1); ConfigPropertyInfo propertyInfo2 = new ConfigPropertyInfo(); propertyInfo2.Name = "Property2"; classInfo.Properties.Add(propertyInfo2); ConfigPropertyInfo propertyInfo3 = new ConfigPropertyInfo(); propertyInfo3.Name = "Property3"; var property3MessagePropertyAttribute = new MessagePropertyAttribute(); property3MessagePropertyAttribute.IsBcd = true; //property3MessagePropertyAttribute.IsAuthenticationField = false; //property3MessagePropertyAttribute.ExcludeFromAuthentication = true; property3MessagePropertyAttribute.Length = 20; property3MessagePropertyAttribute.Format = "YYYYMMDDHHMMSS"; property3MessagePropertyAttribute.BlobType = BlobTypes.None; property3MessagePropertyAttribute.VariableLength = true; property3MessagePropertyAttribute.MinLength = 1; property3MessagePropertyAttribute.MaxLength = 18; property3MessagePropertyAttribute.MinimizeVariableLength = true; //property3MessagePropertyAttribute.ExcludeFromLength = false; property3MessagePropertyAttribute.PrepadCharacter = ' '; //property3MessagePropertyAttribute.MessageLengthType = MessageLengthTypes.RestOfMessage; //property3MessagePropertyAttribute.AuthenticationClass = typeof(AuthenticationCrc32); propertyInfo3.Attributes.Add(property3MessagePropertyAttribute); propertyInfo3.Attributes.Add(new CalculatedLengthResultAttribute() { Exclude = false }); propertyInfo3.Attributes.Add(new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationCrc32))); classInfo.Properties.Add(propertyInfo3); messageSerializerClassInfo.ClassInfo = classInfo; return(messageSerializerClassInfo); }
void VerifyClassInfo(ConfigMessageSerializerClass classInfoToVerify, ConfigMessageSerializerClass classInfoExpected) { Assert.That(classInfoToVerify.ClassInfo, Is.Not.Null); Assert.That(classInfoToVerify.ClassInfo.AssemblyQualifiedName, Is.EqualTo(classInfoExpected.ClassInfo.AssemblyQualifiedName)); VerifyMessageSerializedClassAttribute(classInfoToVerify.ClassInfo.MessageClassAttribute, classInfoExpected.ClassInfo.MessageClassAttribute, classInfoToVerify.ClassInfo.AssemblyQualifiedName); Assert.That(classInfoToVerify.ClassInfo.Properties, Is.Not.Null); Assert.That(classInfoToVerify.ClassInfo.Properties.Count, Is.EqualTo(classInfoExpected.ClassInfo.Properties.Count)); for (int index = 0; index < classInfoToVerify.ClassInfo.Properties.Count; ++index) { ConfigPropertyInfo propertyInfoToVerify = classInfoToVerify.ClassInfo.Properties[index]; ConfigPropertyInfo propertyInfoExpected = classInfoExpected.ClassInfo.Properties[index]; Assert.That(propertyInfoToVerify.Name, Is.EqualTo(propertyInfoExpected.Name)); //VerifyMessageSerializedPropertyAttribute(propertyInfoToVerify._messagePropertyAttribute, propertyInfoExpected._messagePropertyAttribute, string.Format("{0}::Index{1}", classInfoToVerify.ClassInfo, index)); VerifyMessageSerializedPropertyAttribute(propertyInfoToVerify.GetFirstAttributeOfType <MessagePropertyAttribute>() ?? new MessagePropertyAttribute(), propertyInfoExpected.GetFirstAttributeOfType <MessagePropertyAttribute>() ?? new MessagePropertyAttribute(), string.Format("{0}::Index{1}", classInfoToVerify.ClassInfo, index)); VerifyCalculatedLengthResultAttribute(propertyInfoToVerify.GetFirstAttributeOfType <CalculatedLengthResultAttribute>() ?? new CalculatedLengthResultAttribute(), propertyInfoExpected.GetFirstAttributeOfType <CalculatedLengthResultAttribute>() ?? new CalculatedLengthResultAttribute(), string.Format("{0}::Index{1}", classInfoToVerify.ClassInfo, index)); VerifyCalculatedAuthenticationResultAttribute(propertyInfoToVerify.GetFirstAttributeOfType <CalculatedAuthenticationResultAttribute>() ?? new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationCrc16)), propertyInfoExpected.GetFirstAttributeOfType <CalculatedAuthenticationResultAttribute>() ?? new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationCrc16)), string.Format("{0}::Index{1}", classInfoToVerify.ClassInfo, index)); } }
protected TClassType TestDeserialize <TClassType>(byte[] bytesToDeserialize, TClassType originalObject, VerifyDeserializedObjectDelegate verifyFunction, SerializationDefaults serializationDefaults = null) where TClassType : class, T { Serializer.Instance.GetClassInfo(typeof(TClassType), true, serializationDefaults); TClassType deserializedObject = Serializer.Instance.Deserialize <TClassType>(bytesToDeserialize); verifyFunction(deserializedObject, bytesToDeserialize, originalObject); if (_includeFileTesting) { ConfigMessageSerializerClass.WriteDefaultToFile(typeof(TClassType)); ConfigMessageSerializerClass configMessageSerializerClass = ConfigMessageSerializerClass.ReadFromFile(typeof(TClassType)); List <ConfigMessageSerializerClass> classList = new List <ConfigMessageSerializerClass>() { configMessageSerializerClass }; Serializer.Instance.GetClassInfo(typeof(TClassType), classList, true, serializationDefaults); TClassType deserializedObjectConfig = Serializer.Instance.Deserialize <TClassType>(bytesToDeserialize); verifyFunction(deserializedObjectConfig, bytesToDeserialize, originalObject); } return(deserializedObject); }