예제 #1
0
        private bool DelegateParsingToExtender(
            string sourceValue,
            object targetObject,
            Type targetObjectType,
            TypeConverter originalTypeConverter,
            out object retValue)
        {
            retValue = (object)null;
            int num = sourceValue.IndexOf(":");

            if (num < 2)
            {
                return(false);
            }
            string key          = sourceValue.Substring(1, num - 1);
            string sourceValue1 = sourceValue.Substring(num + 1, sourceValue.Length - num - 2);
            Type   type;

            ObjectSerializationInfo.GetBuiltInExtensionMap().TryGetValue(key, out type);
            if ((object)type == null)
            {
                return(false);
            }
            if (!typeof(XmlSerializerExtention).IsAssignableFrom(type))
            {
                throw new NotSupportedException(string.Format("Xml extension type not supported {0}. Extension type must inherit {1}.", (object)type, (object)typeof(XmlSerializerExtention)));
            }
            XmlSerializerExtention instance = (XmlSerializerExtention)Activator.CreateInstance(type);
            XmlSerializerExtensionServiceProvider extensionServiceProvider = new XmlSerializerExtensionServiceProvider(this.serializer.PropertiesProvider, targetObject, (object)this.property, sourceValue1);

            retValue = instance.ProvideValue((IServiceProvider)extensionServiceProvider);
            return(true);
        }
예제 #2
0
        private bool DelegateParsingToExtender(string sourceValue, object targetObject, Type targetObjectType, TypeConverter originalTypeConverter, out object retValue)
        {
            //sample extender strings
            //  {RelativeColor: BaseColor, 0, 10, -10, +30 }
            //  {ParamRef: GradientPercentage}

            retValue = null;

            int separatorIndex = sourceValue.IndexOf(":");
            if (separatorIndex < 2)
            {
                return false;
            }

            string extension = sourceValue.Substring(1, separatorIndex - 1);

            //extract only the value part, excluding extention name and {} brackets: BaseColor, 0, 10, -10, +30
            string valueToParse = sourceValue.Substring(separatorIndex + 1, sourceValue.Length - separatorIndex - 2);

            //MessageBox.Show("extender found: '" + extension + "'");
            //MessageBox.Show("value found: '" + valueToParse + "'");

            Type extensionType;
            GetBuiltInExtensionMap().TryGetValue(extension, out extensionType);

            if (extensionType == null)
            {
                return false;
            }

            if (!typeof(XmlSerializerExtention).IsAssignableFrom(extensionType))
            {
                throw new NotSupportedException(string.Format("Xml extension type not supported {0}. Extension type must inherit {1}.", extensionType, typeof(XmlSerializerExtention)));
            }

            XmlSerializerExtention targetExtention = (XmlSerializerExtention)Activator.CreateInstance(extensionType);

            XmlSerializerExtensionServiceProvider serviceProvider = new XmlSerializerExtensionServiceProvider(
                this.serializer.PropertiesProvider,
                targetObject,
                property,
                valueToParse
                );

            retValue = targetExtention.ProvideValue(serviceProvider);            

            return true;
        }