示例#1
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (value is Padding)
            {
                if (destinationType == typeof(string))
                {
                    Padding padding = (Padding)value;

                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }
                    string        sep          = culture.TextInfo.ListSeparator + " ";
                    TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                    string[]      args         = new string[4];
                    int           nArg         = 0;

                    // Note: ConvertToString will raise exception if value cannot be converted.
                    args[nArg++] = intConverter.ConvertToString(context, culture, padding.Left);
                    args[nArg++] = intConverter.ConvertToString(context, culture, padding.Top);
                    args[nArg++] = intConverter.ConvertToString(context, culture, padding.Right);
                    args[nArg++] = intConverter.ConvertToString(context, culture, padding.Bottom);

                    return(string.Join(sep, args));
                }
                else if (destinationType == typeof(InstanceDescriptor))
                {
                    Padding padding = (Padding)value;

                    if (padding.ShouldSerializeAll())
                    {
                        return(new InstanceDescriptor(
                                   typeof(Padding).GetConstructor(new Type[] { typeof(int) }),
                                   new object[] { padding.All }));
                    }
                    else
                    {
                        return(new InstanceDescriptor(
                                   typeof(Padding).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }),
                                   new object[] { padding.Left, padding.Top, padding.Right, padding.Bottom }));
                    }
                }
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }
示例#2
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
 {
     if (destinationType == null)
     {
         throw new ArgumentNullException("destinationType");
     }
     if (value is Padding)
     {
         if (destinationType == typeof(string))
         {
             Padding padding = (Padding)value;
             if (culture == null)
             {
                 culture = CultureInfo.CurrentCulture;
             }
             string        separator = culture.TextInfo.ListSeparator + " ";
             TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
             string[]      strArray  = new string[4];
             int           num       = 0;
             strArray[num++] = converter.ConvertToString(context, culture, padding.Left);
             strArray[num++] = converter.ConvertToString(context, culture, padding.Top);
             strArray[num++] = converter.ConvertToString(context, culture, padding.Right);
             strArray[num++] = converter.ConvertToString(context, culture, padding.Bottom);
             return(string.Join(separator, strArray));
         }
         if (destinationType == typeof(InstanceDescriptor))
         {
             Padding padding2 = (Padding)value;
             if (padding2.ShouldSerializeAll())
             {
                 return(new InstanceDescriptor(typeof(Padding).GetConstructor(new System.Type[] { typeof(int) }), new object[] { padding2.All }));
             }
             return(new InstanceDescriptor(typeof(Padding).GetConstructor(new System.Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }), new object[] { padding2.Left, padding2.Top, padding2.Right, padding2.Bottom }));
         }
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }