Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more primitive type converters #70057

Merged
merged 3 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ public enum DataObjectMethodType
Insert = 3,
Delete = 4,
}
public partial class DateOnlyConverter : System.ComponentModel.TypeConverter
{
public DateOnlyConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] System.Type? destinationType) { throw null; }
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) { throw null; }
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) { throw null; }
}
public partial class DateTimeConverter : System.ComponentModel.TypeConverter
{
public DateTimeConverter() { }
Expand Down Expand Up @@ -510,6 +518,10 @@ public GuidConverter() { }
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) { throw null; }
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) { throw null; }
}
public partial class HalfConverter : System.ComponentModel.BaseNumberConverter
{
public HalfConverter() { }
}
public partial class HandledEventArgs : System.EventArgs
{
public HandledEventArgs() { }
Expand Down Expand Up @@ -655,6 +667,10 @@ protected InstanceCreationEditor() { }
public virtual string Text { get { throw null; } }
public abstract object? CreateInstance(System.ComponentModel.ITypeDescriptorContext context, System.Type instanceType);
}
public partial class Int128Converter : System.ComponentModel.BaseNumberConverter
{
public Int128Converter() { }
}
public partial class Int16Converter : System.ComponentModel.BaseNumberConverter
{
public Int16Converter() { }
Expand Down Expand Up @@ -1244,6 +1260,14 @@ public static partial class SyntaxCheck
public static bool CheckPath(string value) { throw null; }
public static bool CheckRootedPath(string value) { throw null; }
}
public partial class TimeOnlyConverter : System.ComponentModel.TypeConverter
{
public TimeOnlyConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] System.Type? destinationType) { throw null; }
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) { throw null; }
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) { throw null; }
}
public partial class TimeSpanConverter : System.ComponentModel.TypeConverter
{
public TimeSpanConverter() { }
Expand Down Expand Up @@ -1541,6 +1565,10 @@ public partial class UInt64Converter : System.ComponentModel.BaseNumberConverter
{
public UInt64Converter() { }
}
public partial class UInt128Converter : System.ComponentModel.BaseNumberConverter
{
public UInt128Converter() { }
}
public partial class VersionConverter : System.ComponentModel.TypeConverter
{
public VersionConverter() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
<Compile Include="System\ComponentModel\ByteConverter.cs" />
<Compile Include="System\ComponentModel\CharConverter.cs" />
<Compile Include="System\ComponentModel\CollectionConverter.cs" />
<Compile Include="System\ComponentModel\DateOnlyConverter.cs" />
<Compile Include="System\ComponentModel\DateTimeConverter.cs" />
<Compile Include="System\ComponentModel\DateTimeOffsetConverter.cs" />
<Compile Include="System\ComponentModel\DecimalConverter.cs" />
<Compile Include="System\ComponentModel\DoubleConverter.cs" />
<Compile Include="System\ComponentModel\EnumConverter.cs" />
<Compile Include="System\ComponentModel\GuidConverter.cs" />
<Compile Include="System\ComponentModel\HalfConverter.cs" />
<Compile Include="System\ComponentModel\Int16Converter.cs" />
<Compile Include="System\ComponentModel\Int128Converter.cs" />
<Compile Include="System\ComponentModel\Int32Converter.cs" />
<Compile Include="System\ComponentModel\Int64Converter.cs" />
<Compile Include="System\ComponentModel\ITypeDescriptorContext.cs" />
Expand All @@ -33,9 +36,11 @@
<Compile Include="System\ComponentModel\SByteConverter.cs" />
<Compile Include="System\ComponentModel\SingleConverter.cs" />
<Compile Include="System\ComponentModel\StringConverter.cs" />
<Compile Include="System\ComponentModel\TimeOnlyConverter.cs" />
<Compile Include="System\ComponentModel\TimeSpanConverter.cs" />
<Compile Include="System\ComponentModel\TypeConverter.cs" />
<Compile Include="System\ComponentModel\TypeListConverter.cs" />
<Compile Include="System\ComponentModel\UInt128Converter.cs" />
<Compile Include="System\ComponentModel\UInt16Converter.cs" />
<Compile Include="System\ComponentModel\UInt32Converter.cs" />
<Compile Include="System\ComponentModel\UInt64Converter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

namespace System.ComponentModel
{
/// <summary>
/// Provides a type converter to convert <see cref='System.DateOnly'/>
/// objects to and from various other representations.
/// </summary>
public class DateOnlyConverter : TypeConverter
{
/// <summary>
/// Gets a value indicating whether this converter can convert an
/// object in the given source type to a <see cref='System.DateOnly'/>
/// object using the specified context.
/// </summary>
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

/// <summary>
/// Gets a value indicating whether this converter can convert an object
/// to the given destination type using the context.
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}

/// <summary>
/// Converts the given value object to a <see cref='System.DateOnly'/> object.
/// </summary>
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string text)
{
text = text.Trim();
if (text.Length == 0)
{
return DateOnly.MinValue;
}

try
{
// See if we have a culture info to parse with. If so, then use it.
DateTimeFormatInfo? formatInfo = null;

if (culture != null)
{
formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo));
}

if (formatInfo != null)
{
return DateOnly.Parse(text, formatInfo);
}
else
{
return DateOnly.Parse(text, culture);
}
}
catch (FormatException e)
{
throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, (string)value, nameof(DateOnly)), e);
}
}

return base.ConvertFrom(context, culture, value);
}

/// <summary>
/// Converts the given value object to a <see cref='System.DateOnly'/>
/// object using the arguments.
/// </summary>
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (destinationType == typeof(string) && value is DateOnly dateOnly)
{
if (dateOnly == DateOnly.MinValue)
{
return string.Empty;
}

if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}

DateTimeFormatInfo? formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo));

if (culture == CultureInfo.InvariantCulture)
{
return dateOnly.ToString("yyyy-MM-dd", culture);
}

string format = formatInfo!.ShortDatePattern;

return dateOnly.ToString(format, CultureInfo.CurrentCulture);
}

if (destinationType == typeof(InstanceDescriptor) && value is DateOnly date)
{
return new InstanceDescriptor(typeof(DateOnly).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) }), new object[] { date.Year, date.Month, date.Day });
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;

namespace System.ComponentModel
{
/// <summary>
/// Provides a type converter to convert half-precision, floating point number objects to and
/// from various other representations.
/// </summary>
public class HalfConverter : BaseNumberConverter
{
/// <summary>
/// Determines whether this editor will attempt to convert hex (0x or #) strings
/// </summary>
internal override bool AllowHex => false;

/// <summary>
/// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
/// </summary>
internal override Type TargetType => typeof(Half);

/// <summary>
/// Convert the given value to a string using the given radix
/// </summary>
internal override object FromString(string value, int radix) => throw new NotImplementedException(); // This method shouldn't be called anyway for the Half type as it doesn't support hex formatting.

/// <summary>
/// Convert the given value to a string using the given formatInfo
/// </summary>
internal override object FromString(string value, NumberFormatInfo? formatInfo) => Half.Parse(value, formatInfo);

/// <summary>
/// Convert the given value from a string using the given formatInfo
/// </summary>
internal override string ToString(object value, NumberFormatInfo? formatInfo) =>
((Half)value).ToString(formatInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;

namespace System.ComponentModel
{
/// <summary>
/// Provides a type converter to convert 128-bit signed integer objects to and
/// from various other representations.
/// </summary>
public class Int128Converter : BaseNumberConverter
{
/// <summary>
/// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
/// </summary>
internal override Type TargetType => typeof(Int128);

/// <summary>
/// Convert the given value string to Int128 using the given radix
/// </summary>
internal override object FromString(string value, int radix)
{
Debug.Assert(radix == 16);
Debug.Assert(value is not null);

return Int128.Parse(value, NumberStyles.HexNumber);
}

/// <summary>
/// Convert the given value string to Int128 using given formatInfo
/// </summary>
internal override object FromString(string value, NumberFormatInfo? formatInfo) =>
Int128.Parse(value, formatInfo);

/// <summary>
/// Convert the given value from a string using the given formatInfo
/// </summary>
internal override string ToString(object value, NumberFormatInfo? formatInfo) =>
((Int128)value).ToString(formatInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,22 @@ private static Dictionary<object, IntrinsicTypeConverterData> IntrinsicTypeConve
[typeof(double)] = new IntrinsicTypeConverterData((type) => new DoubleConverter()),
[typeof(string)] = new IntrinsicTypeConverterData((type) => new StringConverter()),
[typeof(int)] = new IntrinsicTypeConverterData((type) => new Int32Converter()),
[typeof(Int128)] = new IntrinsicTypeConverterData((type) => new Int128Converter()),
[typeof(short)] = new IntrinsicTypeConverterData((type) => new Int16Converter()),
[typeof(long)] = new IntrinsicTypeConverterData((type) => new Int64Converter()),
[typeof(float)] = new IntrinsicTypeConverterData((type) => new SingleConverter()),
[typeof(Half)] = new IntrinsicTypeConverterData((type) => new HalfConverter()),
[typeof(UInt128)] = new IntrinsicTypeConverterData((type) => new UInt128Converter()),
[typeof(ushort)] = new IntrinsicTypeConverterData((type) => new UInt16Converter()),
[typeof(uint)] = new IntrinsicTypeConverterData((type) => new UInt32Converter()),
[typeof(ulong)] = new IntrinsicTypeConverterData((type) => new UInt64Converter()),
[typeof(object)] = new IntrinsicTypeConverterData((type) => new TypeConverter()),
[typeof(CultureInfo)] = new IntrinsicTypeConverterData((type) => new CultureInfoConverter()),
[typeof(DateOnly)] = new IntrinsicTypeConverterData((type) => new DateOnlyConverter()),
[typeof(DateTime)] = new IntrinsicTypeConverterData((type) => new DateTimeConverter()),
[typeof(DateTimeOffset)] = new IntrinsicTypeConverterData((type) => new DateTimeOffsetConverter()),
[typeof(decimal)] = new IntrinsicTypeConverterData((type) => new DecimalConverter()),
[typeof(TimeOnly)] = new IntrinsicTypeConverterData((type) => new TimeOnlyConverter()),
[typeof(TimeSpan)] = new IntrinsicTypeConverterData((type) => new TimeSpanConverter()),
[typeof(Guid)] = new IntrinsicTypeConverterData((type) => new GuidConverter()),
[typeof(Uri)] = new IntrinsicTypeConverterData((type) => new UriTypeConverter()),
Expand Down
Loading