protected internal sealed override string SerializeToString(WorkflowMarkupSerializationManager serializationManager, object value)
        {
            if (serializationManager == null)
                throw new ArgumentNullException("serializationManager");
            XmlWriter writer = serializationManager.WorkflowMarkupStack[typeof(XmlWriter)] as XmlWriter;
            if (writer == null)
                throw new ArgumentNullException("writer");
            if (value == null)
                throw new ArgumentNullException("value");

            writer.WriteString(MarkupExtensionSerializer.CompactFormatStart);

            string prefix = String.Empty;
            XmlQualifiedName qualifiedName = serializationManager.GetXmlQualifiedName(value.GetType(), out prefix);
            writer.WriteQualifiedName(qualifiedName.Name, qualifiedName.Namespace);

            int index = 0;

            Dictionary<string, string> constructorArguments = null;
            InstanceDescriptor instanceDescriptor = this.GetInstanceDescriptor(serializationManager, value);
            if (instanceDescriptor != null)
            {
                if (instanceDescriptor.MemberInfo is ConstructorInfo ctorInfo)
                {
                    ParameterInfo[] parameters = ctorInfo.GetParameters();
                    if (parameters != null && parameters.Length == instanceDescriptor.Arguments.Count)
                    {
                        int i = 0;
                        foreach (object argValue in instanceDescriptor.Arguments)
                        {
                            if (constructorArguments == null)
                                constructorArguments = new Dictionary<string, string>();
                            // 
                            if (argValue == null)
                                continue;
                            constructorArguments.Add(parameters[i].Name, parameters[i++].Name);
                            if (index++ > 0)
                                writer.WriteString(MarkupExtensionSerializer.CompactFormatPropertySeperator);
                            else
                                writer.WriteString(MarkupExtensionSerializer.CompactFormatTypeSeperator);
                            if (argValue.GetType() == typeof(string))
                            {
                                writer.WriteString(CreateEscapedValue(argValue as string));
                            }
                            else if (argValue is System.Type)
                            {
                                Type argType = argValue as Type;
                                if (argType.Assembly != null)
                                {
                                    string typePrefix = String.Empty;
                                    XmlQualifiedName typeQualifiedName = serializationManager.GetXmlQualifiedName(argType, out typePrefix);
                                    writer.WriteQualifiedName(XmlConvert.EncodeName(typeQualifiedName.Name), typeQualifiedName.Namespace);
                                }
                                else
                                {
                                    writer.WriteString(argType.FullName);
                                }
                            }
                            else
                            {
                                string stringValue = base.SerializeToString(serializationManager, argValue);
                                if (stringValue != null)
                                    writer.WriteString(stringValue);
                            }
                        }
                    }
                }
            }

            List<PropertyInfo> properties = new List<PropertyInfo>();
            properties.AddRange(GetProperties(serializationManager, value));
            properties.AddRange(serializationManager.GetExtendedProperties(value));
            foreach (PropertyInfo serializableProperty in properties)
            {
                if (Helpers.GetSerializationVisibility(serializableProperty) != DesignerSerializationVisibility.Hidden && serializableProperty.CanRead && serializableProperty.GetValue(value, null) != null)
                {
                    WorkflowMarkupSerializer propSerializer = serializationManager.GetSerializer(serializableProperty.PropertyType, typeof(WorkflowMarkupSerializer)) as WorkflowMarkupSerializer;
                    if (propSerializer == null)
                    {
                        serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNotAvailable, serializableProperty.PropertyType.FullName)));
                        continue;
                    }

                    if (constructorArguments != null)
                    {
                        object[] attributes = serializableProperty.GetCustomAttributes(typeof(ConstructorArgumentAttribute), false);
                        if (attributes.Length > 0 && constructorArguments.ContainsKey((attributes[0] as ConstructorArgumentAttribute).ArgumentName))
                            // Skip this property, it has already been represented by a constructor parameter
                            continue;
                    }

                    //Get the property serializer so that we can convert the bind object to string
                    serializationManager.Context.Push(serializableProperty);
                    try
                    {
                        object propValue = serializableProperty.GetValue(value, null);
                        if (propSerializer.ShouldSerializeValue(serializationManager, propValue))
                        {
                            //We do not allow nested bind syntax
                            if (propSerializer.CanSerializeToString(serializationManager, propValue))
                            {
                                if (index++ > 0)
                                    writer.WriteString(MarkupExtensionSerializer.CompactFormatPropertySeperator);
                                else
                                    writer.WriteString(MarkupExtensionSerializer.CompactFormatTypeSeperator);
                                writer.WriteString(serializableProperty.Name);
                                writer.WriteString(MarkupExtensionSerializer.CompactFormatNameValueSeperator);

                                if (propValue.GetType() == typeof(string))
                                {
                                    writer.WriteString(CreateEscapedValue(propValue as string));
                                }
                                else
                                {
                                    string stringValue = propSerializer.SerializeToString(serializationManager, propValue);
                                    if (stringValue != null)
                                        writer.WriteString(stringValue);
                                }
                            }
                            else
                            {
                                serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, new object[] { serializableProperty.Name, value.GetType().FullName })));
                            }
                        }
                    }
                    catch
                    {
                        serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString(SR.Error_SerializerNoSerializeLogic, new object[] { serializableProperty.Name, value.GetType().FullName })));
                        continue;
                    }
                    finally
                    {
                        Debug.Assert((PropertyInfo)serializationManager.Context.Current == serializableProperty, "Serializer did not remove an object it pushed into stack.");
                        serializationManager.Context.Pop();
                    }
                }
            }
            writer.WriteString(MarkupExtensionSerializer.CompactFormatEnd);
            return string.Empty;

        }
Пример #2
0
        protected internal sealed override string SerializeToString(WorkflowMarkupSerializationManager serializationManager, object value)
        {
            if (serializationManager == null)
            {
                throw new ArgumentNullException("serializationManager");
            }
            XmlWriter writer = serializationManager.WorkflowMarkupStack[typeof(XmlWriter)] as XmlWriter;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            writer.WriteString("{");
            string           prefix           = string.Empty;
            XmlQualifiedName xmlQualifiedName = serializationManager.GetXmlQualifiedName(value.GetType(), out prefix);

            writer.WriteQualifiedName(xmlQualifiedName.Name, xmlQualifiedName.Namespace);
            int num = 0;
            Dictionary <string, string> dictionary         = null;
            InstanceDescriptor          instanceDescriptor = this.GetInstanceDescriptor(serializationManager, value);

            if (instanceDescriptor != null)
            {
                ConstructorInfo memberInfo = instanceDescriptor.MemberInfo as ConstructorInfo;
                if (memberInfo != null)
                {
                    ParameterInfo[] parameters = memberInfo.GetParameters();
                    if ((parameters != null) && (parameters.Length == instanceDescriptor.Arguments.Count))
                    {
                        int index = 0;
                        foreach (object obj2 in instanceDescriptor.Arguments)
                        {
                            if (dictionary == null)
                            {
                                dictionary = new Dictionary <string, string>();
                            }
                            if (obj2 != null)
                            {
                                dictionary.Add(parameters[index].Name, parameters[index++].Name);
                                if (num++ > 0)
                                {
                                    writer.WriteString(",");
                                }
                                else
                                {
                                    writer.WriteString(" ");
                                }
                                if (obj2.GetType() == typeof(string))
                                {
                                    writer.WriteString(this.CreateEscapedValue(obj2 as string));
                                }
                                else if (obj2 is Type)
                                {
                                    Type type = obj2 as Type;
                                    if (type.Assembly != null)
                                    {
                                        string           str2  = string.Empty;
                                        XmlQualifiedName name2 = serializationManager.GetXmlQualifiedName(type, out str2);
                                        writer.WriteQualifiedName(XmlConvert.EncodeName(name2.Name), name2.Namespace);
                                    }
                                    else
                                    {
                                        writer.WriteString(type.FullName);
                                    }
                                }
                                else
                                {
                                    string text = base.SerializeToString(serializationManager, obj2);
                                    if (text != null)
                                    {
                                        writer.WriteString(text);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            List <PropertyInfo> list = new List <PropertyInfo>();

            list.AddRange(this.GetProperties(serializationManager, value));
            list.AddRange(serializationManager.GetExtendedProperties(value));
            foreach (PropertyInfo info2 in list)
            {
                if (((Helpers.GetSerializationVisibility(info2) != DesignerSerializationVisibility.Hidden) && info2.CanRead) && (info2.GetValue(value, null) != null))
                {
                    WorkflowMarkupSerializer serializer = serializationManager.GetSerializer(info2.PropertyType, typeof(WorkflowMarkupSerializer)) as WorkflowMarkupSerializer;
                    if (serializer == null)
                    {
                        serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailable", new object[] { info2.PropertyType.FullName })));
                    }
                    else
                    {
                        if (dictionary != null)
                        {
                            object[] customAttributes = info2.GetCustomAttributes(typeof(ConstructorArgumentAttribute), false);
                            if ((customAttributes.Length > 0) && dictionary.ContainsKey((customAttributes[0] as ConstructorArgumentAttribute).ArgumentName))
                            {
                                continue;
                            }
                        }
                        serializationManager.Context.Push(info2);
                        try
                        {
                            object obj3 = info2.GetValue(value, null);
                            if (serializer.ShouldSerializeValue(serializationManager, obj3))
                            {
                                if (serializer.CanSerializeToString(serializationManager, obj3))
                                {
                                    if (num++ > 0)
                                    {
                                        writer.WriteString(",");
                                    }
                                    else
                                    {
                                        writer.WriteString(" ");
                                    }
                                    writer.WriteString(info2.Name);
                                    writer.WriteString("=");
                                    if (obj3.GetType() == typeof(string))
                                    {
                                        writer.WriteString(this.CreateEscapedValue(obj3 as string));
                                    }
                                    else
                                    {
                                        string str4 = serializer.SerializeToString(serializationManager, obj3);
                                        if (str4 != null)
                                        {
                                            writer.WriteString(str4);
                                        }
                                    }
                                }
                                else
                                {
                                    serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNoSerializeLogic", new object[] { info2.Name, value.GetType().FullName })));
                                }
                            }
                        }
                        catch
                        {
                            serializationManager.ReportError(new WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNoSerializeLogic", new object[] { info2.Name, value.GetType().FullName })));
                        }
                        finally
                        {
                            serializationManager.Context.Pop();
                        }
                    }
                }
            }
            writer.WriteString("}");
            return(string.Empty);
        }