public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor)
 {
     if (_baseContext != null)
     {
         return(_baseContext.GetValueSerializerFor(descriptor));
     }
     else
     {
         return(null);
     }
 }
Exemple #2
0
        // Token: 0x060015FE RID: 5630 RVA: 0x0006D150 File Offset: 0x0006B350
        public override string ConvertToString(object value, IValueSerializerContext context)
        {
            if (value == null)
            {
                return(string.Empty);
            }
            RoutedCommand routedCommand = value as RoutedCommand;

            if (routedCommand == null || !(null != routedCommand.OwnerType))
            {
                throw base.GetConvertToException(value, typeof(string));
            }
            if (CommandConverter.IsKnownType(routedCommand.OwnerType))
            {
                return(routedCommand.Name);
            }
            if (context == null)
            {
                throw new InvalidOperationException(SR.Get("ValueSerializerContextUnavailable", new object[]
                {
                    base.GetType().Name
                }));
            }
            ValueSerializer valueSerializerFor = context.GetValueSerializerFor(typeof(Type));

            if (valueSerializerFor == null)
            {
                throw new InvalidOperationException(SR.Get("TypeValueSerializerUnavailable", new object[]
                {
                    base.GetType().Name
                }));
            }
            return(valueSerializerFor.ConvertToString(routedCommand.OwnerType, context) + "." + routedCommand.Name + "Command");
        }
Exemple #3
0
        // Token: 0x060015FC RID: 5628 RVA: 0x0006D0B4 File Offset: 0x0006B2B4
        public override bool CanConvertToString(object value, IValueSerializerContext context)
        {
            if (context == null || context.GetValueSerializerFor(typeof(Type)) == null)
            {
                return(false);
            }
            RoutedCommand routedCommand = value as RoutedCommand;

            if (routedCommand == null || routedCommand.OwnerType == null)
            {
                return(false);
            }
            if (CommandConverter.IsKnownType(routedCommand.OwnerType))
            {
                return(true);
            }
            string       name      = routedCommand.Name + "Command";
            Type         ownerType = routedCommand.OwnerType;
            string       name2     = ownerType.Name;
            PropertyInfo property  = ownerType.GetProperty(name, BindingFlags.Static | BindingFlags.Public);

            if (property != null)
            {
                return(true);
            }
            FieldInfo field = ownerType.GetField(name, BindingFlags.Static | BindingFlags.Public);

            return(field != null);
        }
        public override object ConvertFromString(string value, IValueSerializerContext context)
        {
            if (value != null)
            {
                if (value != String.Empty)
                {
                    Type   declaringType = null;
                    String commandName;

                    // Check for "ns:Class.Command" syntax.

                    int dotIndex = value.IndexOf('.');
                    if (dotIndex >= 0)
                    {
                        // We have "ns:Class.Command" syntax.

                        // Find the type name in the form of "ns:Class".
                        string typeName = value.Substring(0, dotIndex);

                        if (context == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name));
                        }

                        // Get the ValueSerializer for the System.Type type
                        ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
                        if (typeSerializer == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name));
                        }


                        // Use the TypeValueSerializer to parse the "ns:Class" into a System.Type.
                        declaringType = typeSerializer.ConvertFromString(typeName, context) as Type;

                        // Strip out the "Command" part of "ns:Class.Command".
                        commandName = value.Substring(dotIndex + 1).Trim();
                    }
                    else
                    {
                        // Assume the known commands
                        commandName = value.Trim();
                    }

                    // Find the command given the declaring type & name (this is shared with CommandConverter)
                    ICommand command = CommandConverter.ConvertFromHelper(declaringType, commandName);

                    if (command != null)
                    {
                        return(command);
                    }
                }
                else
                {
                    return(null); // String.Empty <==> null , (for roundtrip cases where Command property values are null)
                }
            }

            return(base.ConvertFromString(value, context));
        }
        // untested
        public static ValueSerializer GetSerializerFor(PropertyInfo descriptor, IValueSerializerContext context)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (context != null)
            {
                return(context.GetValueSerializerFor(descriptor));
            }

            var typeConverterInfo = descriptor.GetCustomAttribute <TypeConverterAttribute> ();
            var typeConverterName = typeConverterInfo?.ConverterTypeName;

            if (string.IsNullOrEmpty(typeConverterName))
            {
                return(null);
            }
            var tcType = Type.GetType(typeConverterName);
            var tc     = Activator.CreateInstance(tcType) as TypeConverter;

            if (tc != null && tc.GetType() != typeof(TypeConverter))
            {
                return(new TypeConverterValueSerializer(tc));
            }
            return(null);
        }
 public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
 {
     ValueSerializer s = context.GetValueSerializerFor (type);
     if (s == null) {
         DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
         s = defaultContext.GetValueSerializerFor (type);
     }
     return s;
 }
 public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor, IValueSerializerContext context)
 {
     ValueSerializer s = context.GetValueSerializerFor (descriptor);
     if (s == null) {
         DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
         s = defaultContext.GetValueSerializerFor (descriptor);
     }
     return s;
 }
        public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (context != null)
            {
                return(context.GetValueSerializerFor(type));
            }

            // Standard MarkupExtensions are serialized without ValueSerializer.
            if (typeof(MarkupExtension).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) && XamlLanguage.AllTypes.Any(x => x.UnderlyingType == type))
            {
                return(null);
            }

            // DateTime is documented as special.
            if (type == typeof(DateTime))
            {
                return(new DateTimeValueSerializer());
            }
            // String too.
            if (type == typeof(string))
            {
                return(new StringValueSerializer());
            }

            // FIXME: this is hack. The complete condition is fully documented at http://msdn.microsoft.com/en-us/library/ms590363.aspx
            if (type.GetTypeInfo().GetCustomAttribute <TypeConverterAttribute> (true) != null)
            {
                var tc = type.GetTypeConverter();
                if (tc != null && tc.GetType() != typeof(TypeConverter))
                {
                    return(new TypeConverterValueSerializer(tc));
                }
            }

            // Undocumented, but System.Type seems also special. While other MarkupExtension returned types are not handled specially, this method returns a valid instance for System.Type. Note that it doesn't for TypeExtension.
            if (type == typeof(Type))
            {
                // Since System.Type does not have a valid TypeConverter, I use TypeExtensionConverter (may sound funny considering the above notes!) for this serializer.
                return(new TypeValueSerializer());
            }

            // Undocumented, but several primitive types get a valid serializer while it does not have TypeConverter.
            // There is still exceptional type! TimeSpan. Why aren't they documented?
            if (type != typeof(object) || type == typeof(TimeSpan))
            {
                var typeConverter = type.GetTypeConverter();
                if (typeConverter != null)
                {
                    return(new TypeConverterValueSerializer(typeConverter));
                }
            }
            return(null);
        }
        /// <summary>
        ///     TypeConverter method implementation.
        /// </summary>
        /// <param name="context">
        ///     ITypeDescriptorContext
        /// </param>
        /// <param name="culture">
        ///     current culture (see CLR specs)
        /// </param>
        /// <param name="value">
        ///     value to convert from
        /// </param>
        /// <param name="destinationType">
        ///     Type to convert to
        /// </param>
        /// <returns>
        ///     converted value
        /// </returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            // Input validation

            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }


            if (destinationType == typeof(MarkupExtension)
                &&
                CanConvertTo(context, destinationType))
            {
                SystemResourceKeyID keyId;

                // Get the SystemResourceKeyID

                if (value is SystemResourceKey)
                {
                    keyId = (value as SystemResourceKey).InternalKey;
                }
                else if (value is SystemThemeKey)
                {
                    keyId = (value as SystemThemeKey).InternalKey;
                }
                else
                {
                    throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "SystemResourceKey or SystemThemeKey"));
                }

                // System resource keys can be converted into a MarkupExtension (StaticExtension)

                Type keyType = SystemKeyConverter.GetSystemClassType(keyId);

                // Get the value serialization context
                IValueSerializerContext valueSerializerContext = context as IValueSerializerContext;
                if (valueSerializerContext != null)
                {
                    // And from that get a System.Type serializer
                    ValueSerializer typeSerializer = valueSerializerContext.GetValueSerializerFor(typeof(Type));
                    if (typeSerializer != null)
                    {
                        // And use that to create the string-ized class name
                        string systemClassName = typeSerializer.ConvertToString(keyType, valueSerializerContext);

                        // And finally create the StaticExtension.
                        return(new StaticExtension(systemClassName + "." + GetSystemKeyName(keyId)));
                    }
                }
            }

            return(base.CanConvertTo(context, destinationType));
        }
Exemple #10
0
        public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor, IValueSerializerContext context)
        {
            ValueSerializer s = context.GetValueSerializerFor(descriptor);

            if (s == null)
            {
                DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
                s = defaultContext.GetValueSerializerFor(descriptor);
            }
            return(s);
        }
Exemple #11
0
        public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
        {
            ValueSerializer s = context.GetValueSerializerFor(type);

            if (s == null)
            {
                DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
                s = defaultContext.GetValueSerializerFor(type);
            }
            return(s);
        }
Exemple #12
0
		// untested
		public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor, IValueSerializerContext context)
		{
			if (descriptor == null)
				throw new ArgumentNullException ("descriptor");
			if (context != null)
				return context.GetValueSerializerFor (descriptor);

			var tc = descriptor.Converter;
			if (tc != null && tc.GetType () != typeof (TypeConverter))
				return new TypeConverterValueSerializer (tc);
			return null;
		}
Exemple #13
0
 /// <summary>
 /// Get the value serializer declared for the given property. ValueSerializer can be overriden by an attribute
 /// on the property declaration. This version should be called whenever the caller has a
 /// IValueSerializerContext to ensure that the correct value serializer is returned for the given context.
 /// </summary>
 /// <param name="descriptor">PropertyDescriptor for the property to be serialized</param>
 /// <param name="context">Context information</param>
 /// <returns>A value serializer associated with the given property</returns>
 public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor, IValueSerializerContext context)
 {
     if (context != null)
     {
         ValueSerializer result = context.GetValueSerializerFor(descriptor);
         if (result != null)
         {
             return(result);
         }
     }
     return(GetSerializerFor(descriptor));
 }
Exemple #14
0
 /// <summary>
 /// Get the value serializer declared for the given type. This version should be called whenever the caller
 /// has a IValueSerializerContext to ensure that the correct value serializer is returned for the given
 /// context.
 /// </summary>
 /// <param name="type">The value type to serialize</param>
 /// <param name="context">Context information</param>
 /// <returns>The value serializer associated with the given type</returns>
 public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
 {
     if (context != null)
     {
         ValueSerializer result = context.GetValueSerializerFor(type);
         if (result != null)
         {
             return(result);
         }
     }
     return(GetSerializerFor(type));
 }
Exemple #15
0
 /// <summary>Gets the <see cref="T:System.Windows.Markup.ValueSerializer" /> declared for the specified type, using the specified context.</summary>
 /// <returns>The serializer associated with the specified type.</returns>
 /// <param name="type">The type to get the <see cref="T:System.Windows.Markup.ValueSerializer" /> for.</param>
 /// <param name="context">Context information that is used for conversion.</param>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="type" /> is null.</exception>
 public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
 {
     if (context != null)
     {
         ValueSerializer valueSerializerFor = context.GetValueSerializerFor(type);
         if (valueSerializerFor != null)
         {
             return(valueSerializerFor);
         }
     }
     return(GetSerializerFor(type));
 }
		// untested
		public static ValueSerializer GetSerializerFor (PropertyInfo descriptor, IValueSerializerContext context)
		{
			if (descriptor == null)
				throw new ArgumentNullException ("descriptor");
			if (context != null)
				return context.GetValueSerializerFor (descriptor);

			var typeConverterInfo = descriptor.GetCustomAttribute<TypeConverterAttribute> ();
			var typeConverterName = typeConverterInfo?.ConverterTypeName;
			if (string.IsNullOrEmpty (typeConverterName))
				return null;
			var tcType = Type.GetType(typeConverterName);
			var tc = Activator.CreateInstance (tcType) as TypeConverter;
			if (tc != null && tc.GetType () != typeof (TypeConverter))
				return new TypeConverterValueSerializer (tc);
			return null;
		}
Exemple #17
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == typeof(MarkupExtension))
     {
         AdornerResourceKey      adornerResourceKey = value as AdornerResourceKey;
         IValueSerializerContext context1           = context as IValueSerializerContext;
         if (adornerResourceKey != null && context1 != null)
         {
             ValueSerializer valueSerializerFor = context1.GetValueSerializerFor(typeof(Type));
             if (valueSerializerFor != null)
             {
                 return((object)new StaticExtension(valueSerializerFor.ConvertToString((object)adornerResourceKey._type, context1) + "." + adornerResourceKey._member));
             }
         }
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Exemple #18
0
		public static ValueSerializer GetSerializerFor (Type type, IValueSerializerContext context)
		{
			if (type == null)
				throw new ArgumentNullException ("type");
			if (context != null)
				return context.GetValueSerializerFor (type);

			// Standard MarkupExtensions are serialized without ValueSerializer.
			if (typeof (MarkupExtension).IsAssignableFrom (type) && XamlLanguage.AllTypes.Any (x => x.UnderlyingType == type))
				return null;

			// DateTime is documented as special.
			if (type == typeof (DateTime))
				return new DateTimeValueSerializer ();
			// String too.
			if (type == typeof (string))
				return new StringValueSerializer ();

			// FIXME: this is hack. The complete condition is fully documented at http://msdn.microsoft.com/en-us/library/ms590363.aspx
			if (type.GetCustomAttribute<TypeConverterAttribute> (true) != null) {
				var tc = type.GetTypeConverter ();
				if (tc != null && tc.GetType () != typeof (TypeConverter))
					return new TypeConverterValueSerializer (tc);
			}

			// Undocumented, but System.Type seems also special. While other MarkupExtension returned types are not handled specially, this method returns a valid instance for System.Type. Note that it doesn't for TypeExtension.
			if (type == typeof (Type))
				// Since System.Type does not have a valid TypeConverter, I use TypeExtensionConverter (may sound funny considering the above notes!) for this serializer.
				return new TypeValueSerializer ();

			// Undocumented, but several primitive types get a valid serializer while it does not have TypeConverter.
			switch (Type.GetTypeCode (type)) {
			case TypeCode.Object:
			case TypeCode.DBNull:
				break;
			default:
				return new TypeConverterValueSerializer (type.GetTypeConverter ());
			}

			// There is still exceptional type! TimeSpan. Why aren't they documented?
			if (type == typeof (TimeSpan))
				return new TypeConverterValueSerializer (type.GetTypeConverter ());

			return null;
		}
Exemple #19
0
 // Token: 0x06001600 RID: 5632 RVA: 0x0006D280 File Offset: 0x0006B480
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (value != null)
     {
         if (!(value != string.Empty))
         {
             return(null);
         }
         Type   ownerType = null;
         int    num       = value.IndexOf('.');
         string localName;
         if (num >= 0)
         {
             string value2 = value.Substring(0, num);
             if (context == null)
             {
                 throw new InvalidOperationException(SR.Get("ValueSerializerContextUnavailable", new object[]
                 {
                     base.GetType().Name
                 }));
             }
             ValueSerializer valueSerializerFor = context.GetValueSerializerFor(typeof(Type));
             if (valueSerializerFor == null)
             {
                 throw new InvalidOperationException(SR.Get("TypeValueSerializerUnavailable", new object[]
                 {
                     base.GetType().Name
                 }));
             }
             ownerType = (valueSerializerFor.ConvertFromString(value2, context) as Type);
             localName = value.Substring(num + 1).Trim();
         }
         else
         {
             localName = value.Trim();
         }
         ICommand command = CommandConverter.ConvertFromHelper(ownerType, localName);
         if (command != null)
         {
             return(command);
         }
     }
     return(base.ConvertFromString(value, context));
 }
Exemple #20
0
        // untested
        public static ValueSerializer GetSerializerFor(PropertyInfo descriptor, IValueSerializerContext context)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (context != null)
            {
                return(context.GetValueSerializerFor(descriptor));
            }

            var tc = descriptor.GetTypeConverter();

            if (tc != null && !tc.IsBaseTypeConverter())
            {
                return(new TypeConverterValueSerializer(tc));
            }
            return(null);
        }
Exemple #21
0
        // untested
        public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor, IValueSerializerContext context)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (context != null)
            {
                return(context.GetValueSerializerFor(descriptor));
            }

            var tc = descriptor.Converter;

            if (tc != null && tc.GetType() != typeof(TypeConverter))
            {
                return(new TypeConverterValueSerializer(tc));
            }
            return(null);
        }
        // Token: 0x06002433 RID: 9267 RVA: 0x000B0678 File Offset: 0x000AE878
        internal static object CheckForMarkupExtension(Type propertyType, object value, IValueSerializerContext context, bool convertEnums)
        {
            if (value == null)
            {
                return(new NullExtension());
            }
            TypeConverter converter = TypeDescriptor.GetConverter(value);

            if (converter.CanConvertTo(context, typeof(MarkupExtension)))
            {
                return(converter.ConvertTo(context, TypeConverterHelper.InvariantEnglishUS, value, typeof(MarkupExtension)));
            }
            Type type = value as Type;

            if (type != null)
            {
                if (propertyType == typeof(Type))
                {
                    return(value);
                }
                return(new TypeExtension(type));
            }
            else
            {
                if (convertEnums)
                {
                    Enum @enum = value as Enum;
                    if (@enum != null)
                    {
                        ValueSerializer valueSerializerFor = context.GetValueSerializerFor(typeof(Type));
                        string          str = valueSerializerFor.ConvertToString(@enum.GetType(), context);
                        return(new StaticExtension(str + "." + @enum.ToString()));
                    }
                }
                Array array = value as Array;
                if (array != null)
                {
                    return(new ArrayExtension(array));
                }
                return(value);
            }
        }
        public override bool CanConvertToString(object value, IValueSerializerContext context)
        {
            if (context == null || context.GetValueSerializerFor(typeof(Type)) == null)
            {
                return(false);
            }

            // Can only convert routed commands
            RoutedCommand command = value as RoutedCommand;

            if (command == null || command.OwnerType == null)
            {
                return(false);
            }

            if (CommandConverter.IsKnownType(command.OwnerType))
            {
                return(true);
            }
            else
            {
                string localName = command.Name + "Command";
                Type   ownerType = command.OwnerType;
                string typeName  = ownerType.Name;

                // Get them from Properties
                PropertyInfo propertyInfo = ownerType.GetProperty(localName, BindingFlags.Public | BindingFlags.Static);
                if (propertyInfo != null)
                {
                    return(true);
                }

                // Get them from Fields (ScrollViewer.PageDownCommand is a static readonly field
                FieldInfo fieldInfo = ownerType.GetField(localName, BindingFlags.Static | BindingFlags.Public);
                if (fieldInfo != null)
                {
                    return(true);
                }
            }

            return(false);
        }
        public override string ConvertToString(object value, IValueSerializerContext context)
        {
            if (value != null)
            {
                RoutedCommand command = value as RoutedCommand;
                if (null != command && null != command.OwnerType)
                {
                    // Known Commands, so write shorter version
                    if (CommandConverter.IsKnownType(command.OwnerType))
                    {
                        return(command.Name);
                    }
                    else
                    {
                        ValueSerializer typeSerializer = null;

                        if (context == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name));
                        }

                        // Get the ValueSerializer for the System.Type type
                        typeSerializer = context.GetValueSerializerFor(typeof(Type));
                        if (typeSerializer == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name));
                        }

                        return(typeSerializer.ConvertToString(command.OwnerType, context) + "." + command.Name + "Command");
                    }
                }
            }
            else
            {
                return(string.Empty);
            }

            throw GetConvertToException(value, typeof(string));
        }
 // Token: 0x06002403 RID: 9219 RVA: 0x000AF8C4 File Offset: 0x000ADAC4
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == null)
     {
         throw new ArgumentNullException("destinationType");
     }
     if (destinationType == typeof(MarkupExtension) && this.CanConvertTo(context, destinationType))
     {
         SystemResourceKeyID internalKey;
         if (value is SystemResourceKey)
         {
             internalKey = (value as SystemResourceKey).InternalKey;
         }
         else
         {
             if (!(value is SystemThemeKey))
             {
                 throw new ArgumentException(SR.Get("MustBeOfType", new object[]
                 {
                     "value",
                     "SystemResourceKey or SystemThemeKey"
                 }));
             }
             internalKey = (value as SystemThemeKey).InternalKey;
         }
         Type systemClassType = SystemKeyConverter.GetSystemClassType(internalKey);
         IValueSerializerContext valueSerializerContext = context as IValueSerializerContext;
         if (valueSerializerContext != null)
         {
             ValueSerializer valueSerializerFor = valueSerializerContext.GetValueSerializerFor(typeof(Type));
             if (valueSerializerFor != null)
             {
                 string str = valueSerializerFor.ConvertToString(systemClassType, valueSerializerContext);
                 return(new StaticExtension(str + "." + SystemKeyConverter.GetSystemKeyName(internalKey)));
             }
         }
     }
     return(base.CanConvertTo(context, destinationType));
 }
        public override bool CanConvertToString(object value, IValueSerializerContext context)
        {
            if (context == null || context.GetValueSerializerFor(typeof(Type)) == null)
                return false;

            // Can only convert routed commands
            RoutedCommand command = value as RoutedCommand;

            if (command == null || command.OwnerType == null)
            {
                return false;
            }
            
            if (CommandConverter.IsKnownType(command.OwnerType))
            {
                return true;
            }
            else
            {
                string localName = command.Name + "Command";
                Type ownerType = command.OwnerType;
                string typeName = ownerType.Name;

                // Get them from Properties
                PropertyInfo propertyInfo = ownerType.GetProperty(localName, BindingFlags.Public | BindingFlags.Static);
                if (propertyInfo != null)
                    return true;

                // Get them from Fields (ScrollViewer.PageDownCommand is a static readonly field
                FieldInfo fieldInfo = ownerType.GetField(localName, BindingFlags.Static | BindingFlags.Public);
                if (fieldInfo != null)
                    return true;
            }

            return false; 
        }
        //
        //  Check for values that can be converted into markup extensions, either because
        //  they are a well known type ((null, arrays, enums, and types), or because they
        //  can be type converted into an ME.
        //

        internal static object CheckForMarkupExtension(
            Type propertyType,
            object value,
            IValueSerializerContext context,
            bool convertEnums)
        {
            // null => NullExtension

            if (value == null)
            {
                return(new NullExtension());
            }

            // See if the type has a type converter that can create a MarkupExtension
            // (Have to do this after the null check so that GetConverter doesn't get
            // an invalid argument.)

            TypeConverter converter = TypeDescriptor.GetConverter(value);

            if (converter.CanConvertTo(context, typeof(MarkupExtension)))
            {
                // The type provides a converter that creates a MarkupExtension.
                return(converter.ConvertTo(context, TypeConverterHelper.InvariantEnglishUS, value, typeof(MarkupExtension)));
            }

            // System.Type => TypeExtension

            Type type = value as Type;

            if (type != null)
            {
                // If the property is declared to be a type already, we don't need to convert it
                // into {x:Type} syntax.

                if (propertyType == typeof(Type))
                {
                    return(value);
                }

                return(new TypeExtension(type));
            }

            // Enums => StaticExtension

            if (convertEnums)
            {
                Enum enumValue = value as Enum;
                if (enumValue != null)
                {
                    ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
                    Debug.Assert(typeSerializer != null, "typeSerializer for Enum was null");
                    string typeName = typeSerializer.ConvertToString(enumValue.GetType(), context);
                    return(new StaticExtension(typeName + "." + enumValue.ToString()));
                }
            }

            // Arrays => ArrayExtension

            Array array = value as Array;

            if (array != null)
            {
                return(new ArrayExtension(array));
            }

            // Otherwise, value is unchanged.

            return(value);
        }
        private string ConvertMarkupItemToString(MarkupObject item)
        {
            ValueSerializer typeSerializer = _context.GetValueSerializerFor(typeof(Type));

            Debug.Assert(typeSerializer != null, "Could not retrieve typeSerializer for Type");

            // Serialize the markup extension into a string
            StringBuilder resultBuilder = new StringBuilder();

            resultBuilder.Append('{');

            string typeName = typeSerializer.ConvertToString(item.ObjectType, _context);

            if (typeName.EndsWith("Extension", StringComparison.Ordinal))
            {
                // The "Extension" suffix is optional, much like the Attribute suffix of an Attribute.
                // The normalized version is without the suffix.
                resultBuilder.Append(typeName, 0, typeName.Length - EXTENSIONLENGTH);
            }
            else
            {
                resultBuilder.Append(typeName);
            }

            bool first           = true;
            bool propertyWritten = false;

            foreach (MarkupProperty property in item.Properties)
            {
                resultBuilder.Append(first ? " " : ", ");

                first = false;
                if (!property.IsConstructorArgument)
                {
                    resultBuilder.Append(property.Name);
                    resultBuilder.Append('=');
                    propertyWritten = true;
                }
                else
                {
                    Debug.Assert(!propertyWritten, "An argument was returned after a property was set. All arguments must be returned first and in order");
                }

                string value = property.StringValue;

                if (value != null && value.Length > 0)
                {
                    if (value[0] == '{')
                    {
                        if (value.Length > 1 && value[1] == '}')
                        {
                            // It is a literal quote, remove the literals and write the text with escapes.
                            value = value.Substring(2);
                        }
                        else
                        {
                            // It is a nested markup-extension, just insert the text literally.
                            resultBuilder.Append(value);
                            continue;
                        }
                    }

                    // Escape the string
                    for (int i = 0; i < value.Length; i++)
                    {
                        char ch = value[i];
                        switch (ch)
                        {
                        case '{':
                            resultBuilder.Append(@"\{");
                            break;

                        case '}':
                            resultBuilder.Append(@"\}");
                            break;

                        case ',':
                            resultBuilder.Append(@"\,");
                            break;

                        default:
                            resultBuilder.Append(ch);
                            break;
                        }
                    }
                }
            }
            resultBuilder.Append('}');

            return(resultBuilder.ToString());
        }
        public override string ConvertToString(object value, IValueSerializerContext context)
        {
            if (value != null)
            {
                RoutedCommand command = value as RoutedCommand;
                if (null != command && null != command.OwnerType)
                {
                    // Known Commands, so write shorter version
                    if (CommandConverter.IsKnownType(command.OwnerType))
                    {
                        return command.Name;
                    }
                    else
                    {
                        ValueSerializer typeSerializer = null;

                        if (context == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name ));
                        }

                        // Get the ValueSerializer for the System.Type type
                        typeSerializer = context.GetValueSerializerFor(typeof(Type));
                        if (typeSerializer == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name ));
                        }

                        return typeSerializer.ConvertToString(command.OwnerType, context) + "." + command.Name + "Command";
                    }
                }
            }
            else
                return string.Empty;
            
            throw GetConvertToException(value, typeof(string));
        }
        public override object ConvertFromString(string value, IValueSerializerContext context)
        {
            if (value != null)
            {
                if (value != String.Empty)
                {
                    Type declaringType = null;
                    String commandName;

                    // Check for "ns:Class.Command" syntax.
                    
                    int dotIndex = value.IndexOf('.');
                    if (dotIndex >= 0)
                    {
                        // We have "ns:Class.Command" syntax.

                        // Find the type name in the form of "ns:Class".
                        string typeName = value.Substring(0, dotIndex);

                        if (context == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name ));
                        }

                        // Get the ValueSerializer for the System.Type type
                        ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
                        if (typeSerializer == null)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name ));
                        }


                        // Use the TypeValueSerializer to parse the "ns:Class" into a System.Type.
                        declaringType = typeSerializer.ConvertFromString(typeName, context) as Type;

                        // Strip out the "Command" part of "ns:Class.Command".
                        commandName = value.Substring(dotIndex + 1).Trim();
                    }
                    else
                    {
                        // Assume the known commands
                        commandName = value.Trim();
                    }

                    // Find the command given the declaring type & name (this is shared with CommandConverter)
                    ICommand command = CommandConverter.ConvertFromHelper( declaringType, commandName );

                    if (command != null)
                    {
                        return command;
                    }
                }
                else
                {
                    return null; // String.Empty <==> null , (for roundtrip cases where Command property values are null)
                }
            }

            return base.ConvertFromString(value, context);
        }
        // 
        //  Check for values that can be converted into markup extensions, either because
        //  they are a well known type ((null, arrays, enums, and types), or because they 
        //  can be type converted into an ME. 
        //
 
        internal static object CheckForMarkupExtension(
                                    Type propertyType,
                                    object value,
                                    IValueSerializerContext context, 
                                    bool convertEnums)
        { 
            // null => NullExtension 

            if (value == null) 
            {
                return new NullExtension();
            }
 
            // See if the type has a type converter that can create a MarkupExtension
            // (Have to do this after the null check so that GetConverter doesn't get 
            // an invalid argument.) 

            TypeConverter converter = TypeDescriptor.GetConverter(value); 
            if (converter.CanConvertTo(context, typeof(MarkupExtension)))
            {
                // The type provides a converter that creates a MarkupExtension.
                return converter.ConvertTo(context, TypeConverterHelper.InvariantEnglishUS, value, typeof(MarkupExtension)); 
            }
 
            // System.Type => TypeExtension 

            Type type = value as Type; 
            if (type != null)
            {
                // If the property is declared to be a type already, we don't need to convert it
                // into {x:Type} syntax. 

                if( propertyType == typeof(Type) ) 
                { 
                    return value;
                } 

                return new TypeExtension(type);
            }
 
            // Enums => StaticExtension
 
            if (convertEnums) 
            {
                Enum enumValue = value as Enum; 
                if (enumValue != null)
                {
                    ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
                    Debug.Assert(typeSerializer != null, "typeSerializer for Enum was null"); 
                    string typeName = typeSerializer.ConvertToString(enumValue.GetType(), context);
                    return new StaticExtension(typeName + "." + enumValue.ToString()); 
                } 
            }
 
            // Arrays => ArrayExtension

            Array array = value as Array;
            if (array != null) 
            {
                return new ArrayExtension(array); 
            } 

            // Otherwise, value is unchanged. 

            return value;
        }
 public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor, IValueSerializerContext context)
 {
     if (context != null)
     {
         ValueSerializer valueSerializerFor = context.GetValueSerializerFor(descriptor);
         if (valueSerializerFor != null)
         {
             return valueSerializerFor;
         }
     }
     return GetSerializerFor(descriptor);
 }
 public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
 {
     if (context != null)
     {
         ValueSerializer valueSerializerFor = context.GetValueSerializerFor(type);
         if (valueSerializerFor != null)
         {
             return valueSerializerFor;
         }
     }
     return GetSerializerFor(type);
 }