public static CommandBase CreateCommand(Controller controller, CommandDescriptor commandDescriptor) { CommandBase commandObject = CreateCommandObject(commandDescriptor.CommandName); if (commandObject is StackedCommand) { ((StackedCommand)commandObject).Controller = controller; } FillParameters(commandObject, commandDescriptor); return(commandObject); }
public static void FillParameters(CommandBase commandObject, CommandDescriptor parametersDescriptors) { foreach (ParameterDescriptor parameter in parametersDescriptors.Parameters) { PropertyInfo propertyInfo = parameter.ParameterPropertyInfo; object value = parameter.ParameterValue; if (value == null && parameter.CreateControlInEdtors == false) { continue; } propertyInfo.SetValue(commandObject, value, null); } }
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); }
private static void LoadCommandsTypes() { if (publicCommandsTypes == null) { Assembly assembly = typeof(CommandBase).Assembly; publicCommandsTypes = assembly.GetTypesWithAttribute <PublicCommandAttribute>(); publicCommandsByScope = new Dictionary <ScopeAttribute.EScope, List <CommandDescriptor> >(); publicCommandsByType = new Dictionary <Type, CommandDescriptor>(); foreach (ScopeAttribute.EScope scope in Enum.GetValues(typeof(ScopeAttribute.EScope))) { publicCommandsByScope.CreateSubCollectionIfNeeded(scope); } foreach (Type commandType in publicCommandsTypes) { PublicCommandAttribute a; commandType.TryGetAttribute(out a); CommandDescriptor commandDescriptor = new CommandDescriptor(); commandDescriptor.CommandName = commandType.FullName; commandDescriptor.CommandDescription = a.Name; commandDescriptor.CommandType = commandType; commandDescriptor.Parameters = GetCommandParameters(commandType); publicCommandsByType.Add(commandType, commandDescriptor); foreach (PropertyInfo propertyInfo in commandType.GetProperties()) { PublicArgumentAttribute publicArgumentAttribute; if (propertyInfo.TryGetAttribute(out publicArgumentAttribute)) { if (propertyInfo.PropertyType.IsAmong(typeof(Guid), typeof(List <Guid>))) { if (publicArgumentAttribute.ComponentType == null) { throw new ExolutioCommandException( "When MandatoryArgument is applied on a property of type System.Guid, property ComponentType must be defined for the attribute."); } } else if (publicArgumentAttribute.ComponentType != null) { throw new ExolutioCommandException( "When MandatoryArgument is applied on a property of type other than System.Guid, property ComponentType must not be defined for the attribute."); } } ScopeAttribute scopeAttribute; if (propertyInfo.TryGetAttribute(out scopeAttribute)) { commandDescriptor.ScopeProperty = propertyInfo; commandDescriptor.Scope = scopeAttribute.Scope; ParameterDescriptor scopeParameter = commandDescriptor.Parameters.First(p => p.ParameterPropertyInfo == commandDescriptor.ScopeProperty); scopeParameter.IsScopeParamater = true; #if SILVERLIGHT foreach (ScopeAttribute.EScope scope in EnumHelper.GetValues(typeof(ScopeAttribute.EScope))) #else foreach (ScopeAttribute.EScope scope in Enum.GetValues(typeof(ScopeAttribute.EScope))) #endif { if (scope == ScopeAttribute.EScope.None) { continue; } if (scopeAttribute.Scope.HasFlag(scope)) { publicCommandsByScope.CreateSubCollectionIfNeeded(scope); publicCommandsByScope[scope].Add(commandDescriptor); } } } } if (commandDescriptor.ScopeProperty == null) { publicCommandsByScope.CreateSubCollectionIfNeeded(ScopeAttribute.EScope.None); publicCommandsByScope[ScopeAttribute.EScope.None].Add(commandDescriptor); } if (commandDescriptor.ScopeProperty != null) { foreach (ParameterDescriptor parameterDescriptor in commandDescriptor.Parameters) { if (parameterDescriptor.ModifiedComponentPropertyName != null) { Type componentType = commandDescriptor.Parameters.First(p => p.IsScopeParamater).ComponentType; parameterDescriptor.ModifiedComponentProperty = componentType.GetProperty(parameterDescriptor.ModifiedComponentPropertyName); } } } } publicCommandsTypes.Sort(CommandTypeComparer); } }
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); }