/// <summary>
 /// Set control value from extension object.
 /// </summary>
 /// <param name="extensionObject"></param>
 public void SetControlValuesFromObjectProperties(IExtensionObject extensionObject)
 {
     Dictionary<string, IFieldMetadata> fieldMetadataDictionary = this.fieldMetadataControlBuilders.Keys.ToDictionary(kvp => kvp.Name, kvp => kvp);
     IEnumerator<KeyValuePair<string, object>> iterator = extensionObject.GetFieldEnumerator();
     while (iterator.MoveNext())
     {
         KeyValuePair<string, object> property = iterator.Current;
         if (!fieldMetadataDictionary.ContainsKey(property.Key)) continue;
         IFieldMetadata fieldMetadata = fieldMetadataDictionary[property.Key];
         IExtensionFieldControlBuilder fieldControlBuilder = this.fieldMetadataControlBuilders[fieldMetadata];
         fieldControlBuilder.Value = property.Value;
     }
 }
        /// <summary>
        /// Save extension field serilized into extension field (ExtensionData)
        /// </summary>
        /// <param name="extensionObject"></param>
        public void Serialize(IExtensionObject extensionObject)
        {
            FieldCollection fieldValues = new FieldCollection();
            IEnumerator<KeyValuePair<string, object>> fieldNameValueEnumerator = extensionObject.GetFieldEnumerator();
            while (fieldNameValueEnumerator.MoveNext())
            {
                KeyValuePair<string, object> fieldNameValuePair = fieldNameValueEnumerator.Current;

                using (ValidationScope validationScope = new ValidationScope())
                {
                    try
                    {
                        IFieldValue fieldValue = ConvertToFieldValueInterface(extensionObject.ExtensionDataTypeId, fieldNameValuePair.Key, fieldNameValuePair.Value);
                        if (fieldValue != null) fieldValues.Add(new FieldNameValuePair(fieldNameValuePair.Key, fieldValue));
                    }
                    catch (InvalidFieldValueException exp)
                    {
                        validationScope.Error(exp.Message);
                    }
                    catch (NotSupportedException exp)
                    {
                        validationScope.Error(exp.Message);
                    }
                }
            }

            StringBuilder output = new StringBuilder();

            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8,
                Indent = true
            };

            using (XmlWriter writer = XmlWriter.Create(output, settings))
            {
                serializer.WriteObject(writer, fieldValues);
            }

            extensionObject.ExtensionData = output.ToString();
        }
 /// <summary>
 /// Parse extension properties of extensionObject into current extension object.
 /// </summary>
 /// <param name="extensionObject"></param>
 public void ParseExtensionPropertiesFrom(IExtensionObject extensionObject)
 {
     this.ExtensionDataTypeId = extensionObject.ExtensionDataTypeId;
     IEnumerator<KeyValuePair<string, object>> properties = extensionObject.GetFieldEnumerator();
     while (properties.MoveNext())
         this[properties.Current.Key] = properties.Current.Value;
 }