Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        // 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);
        }
Exemplo n.º 3
0
        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);
        }