MarginsConverter
Provides a MarginsConverter for Margins.
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace System.Drawing.Printing
{
[NullableContext(2)]
[Nullable(0)]
public class MarginsConverter : ExpandableObjectConverter
{
[NullableContext(1)]
public override bool CanConvertFrom([Nullable(2)] ITypeDescriptorContext context, Type sourceType)
{
if (!(sourceType == typeof(string)))
return base.CanConvertFrom(context, sourceType);
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type destinationType)
{
if (!(destinationType == typeof(InstanceDescriptor)))
return base.CanConvertTo(context, destinationType);
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, [Nullable(1)] object value)
{
string text = value as string;
if (text == null)
return base.ConvertFrom(context, culture, value);
string text2 = text.Trim();
if (text2.Length == 0)
return null;
if (culture == null)
culture = CultureInfo.CurrentCulture;
char separator = culture.TextInfo.ListSeparator[0];
string[] array = text2.Split(separator, StringSplitOptions.None);
int[] array2 = new int[array.Length];
TypeConverter intConverter = GetIntConverter();
for (int i = 0; i < array2.Length; i++) {
array2[i] = (int)intConverter.ConvertFromString(context, culture, array[i]);
}
if (array2.Length != 4)
throw new ArgumentException(System.SR.Format(System.SR.TextParseFailedFormat, text2, "left, right, top, bottom"));
return new Margins(array2[0], array2[1], array2[2], array2[3]);
}
[NullableContext(1)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "TypeDescriptor.GetConverter is safe for primitive types.")]
private static TypeConverter GetIntConverter()
{
return TypeDescriptor.GetConverter(typeof(int));
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, [Nullable(1)] Type destinationType)
{
ArgumentNullException.ThrowIfNull(destinationType, "destinationType");
Margins margins = value as Margins;
if ((object)margins != null) {
if (destinationType == typeof(string)) {
if (culture == null)
culture = CultureInfo.CurrentCulture;
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter intConverter = GetIntConverter();
string[] array = new string[4];
int num = 0;
array[num++] = intConverter.ConvertToString(context, culture, margins.Left);
array[num++] = intConverter.ConvertToString(context, culture, margins.Right);
array[num++] = intConverter.ConvertToString(context, culture, margins.Top);
array[num++] = intConverter.ConvertToString(context, culture, margins.Bottom);
return string.Join(separator, array);
}
if (destinationType == typeof(InstanceDescriptor)) {
ConstructorInfo constructor = typeof(Margins).GetConstructor(new Type[4] {
typeof(int),
typeof(int),
typeof(int),
typeof(int)
});
if ((object)constructor != null)
return new InstanceDescriptor(constructor, new object[4] {
margins.Left,
margins.Right,
margins.Top,
margins.Bottom
});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
[NullableContext(1)]
public override object CreateInstance([Nullable(2)] ITypeDescriptorContext context, IDictionary propertyValues)
{
ArgumentNullException.ThrowIfNull(propertyValues, "propertyValues");
object obj = propertyValues["Left"];
object obj2 = propertyValues["Right"];
object obj3 = propertyValues["Top"];
object obj4 = propertyValues["Bottom"];
if (obj is int && obj2 is int && obj4 is int && obj3 is int)
return new Margins((int)obj, (int)obj2, (int)obj3, (int)obj4);
throw new ArgumentException(System.SR.PropertyValueInvalidEntry);
}
}
}