コード例 #1
0
ファイル: CommandSerializer.cs プロジェクト: mff-uk/exolutio
        public static CommandBase DeserializeCommand(XElement commandElement)
        {
            XElement    fullNameElement = commandElement.Element(SERIALIZATION_NS + "FullName");
            CommandBase commandObject   = CreateCommandObject(fullNameElement.Value);

            CommandDescriptor commandParametersDescriptors = PublicCommandsHelper.GetCommandDescriptor(commandObject.GetType());

            foreach (XElement parameterElement in commandElement.Element(SERIALIZATION_NS + "Parameters").Elements(SERIALIZATION_NS + "Parameter"))
            {
                string propertyName              = parameterElement.Element(SERIALIZATION_NS + "PropertyName").Value;
                ParameterDescriptor parameter    = commandParametersDescriptors.GetParameterByPropertyName(propertyName);
                PropertyInfo        propertyInfo = parameter.ParameterPropertyInfo;
                string stringValue = parameterElement.Element(SERIALIZATION_NS + "Value").Value;
                object value       = DeserializePropertyValue(propertyInfo, stringValue);
                parameter.ParameterValue = value;
            }

            FillParameters(commandObject, commandParametersDescriptors);

            return(commandObject);
        }
コード例 #2
0
ファイル: CommandSerializer.cs プロジェクト: mff-uk/exolutio
        private static XElement SerializeRec(CommandBase command, bool isInitial, bool isUndo, bool isRedo)
        {
            Type commandType = command.GetType();

            XElement commandElement;

            commandElement = new XElement(SERIALIZATION_NS + "Command");
            if (isInitial)
            {
                commandElement.Add(new XAttribute("initial", "true"));
            }
            if (isUndo)
            {
                commandElement.Add(new XAttribute("undo", "true"));
            }
            if (isRedo)
            {
                commandElement.Add(new XAttribute("redo", "true"));
            }

            if (command is PropagationMacroCommand)
            {
                commandElement.Add(new XAttribute("propagation", "true"));
            }

            XAttribute nameAttribute = new XAttribute("Name", commandType.Name);

            commandElement.Add(nameAttribute);

            XElement fullNameElement = new XElement(SERIALIZATION_NS + "FullName");

            fullNameElement.Add(new XText(commandType.FullName));
            commandElement.Add(fullNameElement);

            if (command.Report != null && !string.IsNullOrEmpty(command.Report.Contents))
            {
                XElement report = new XElement(SERIALIZATION_NS + "Report");
                report.Add(new XText(command.Report.Contents));
                commandElement.Add(report);
            }

            #region operation parameters
            if (PublicCommandsHelper.IsPublicCommand(commandType))
            {
                CommandDescriptor commandParametersDescriptors = PublicCommandsHelper.GetCommandDescriptor(commandType);
                if (commandParametersDescriptors.Parameters.Count > 0)
                {
                    XElement parametersElement = new XElement(SERIALIZATION_NS + "Parameters");
                    commandElement.Add(parametersElement);
                    foreach (ParameterDescriptor parameter in commandParametersDescriptors.Parameters)
                    {
                        XElement parameterElement = new XElement(SERIALIZATION_NS + "Parameter");
                        parameterElement.Add(new XElement(SERIALIZATION_NS + "Name", parameter.ParameterName));
                        parameterElement.Add(new XElement(SERIALIZATION_NS + "PropertyName", parameter.ParameterPropertyName));

                        object value = parameter.ParameterPropertyInfo.GetValue(command, new object[0]);
                        if (value is Guid)
                        {
                            string idText = value.ToString();
                            parameterElement.Add(new XElement(SERIALIZATION_NS + "Value", idText));
                            if (command is StackedCommand)
                            {
                                Project        p = ((StackedCommand)command).Controller.Project;
                                ExolutioObject component;
                                if (p.TryTranslateObject((Guid)value, out component))
                                {
                                    parameterElement.Add(new XElement(SERIALIZATION_NS + "ValueText", component.ToString()));
                                }
                            }
                        }
                        else if (value is ExolutioObject)
                        {
                            string idText = ((ExolutioObject)value).ID.ToString();
                            parameterElement.Add(new XElement(SERIALIZATION_NS + "ValueID", idText));
                            parameterElement.Add(new XElement(SERIALIZATION_NS + "ValueText", idText));
                        }
                        else
                        {
                            string valueText = value.ToString();
                            parameterElement.Add(new XElement(SERIALIZATION_NS + "Value", valueText));
                        }
                        parametersElement.Add(parameterElement);
                    }
                }
            }

            #endregion

            if (command is MacroCommand && ((MacroCommand)command).Commands.Count > 0)
            {
                XElement subCommandsElement = new XElement(SERIALIZATION_NS + "SubCommands");
                subCommandsElement.Add(new XAttribute("count", ((MacroCommand)command).Commands.Count));
                commandElement.Add(subCommandsElement);
                foreach (CommandBase subCommand in ((MacroCommand)command).Commands)
                {
                    SerializeRec(subCommand, false, isUndo, isRedo);
                }
            }

            return(commandElement);
        }