-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: new System.ComponentModel.DateOnlyConverter and System.ComponentModel.TimeOnlyConverter classes #68743
Comments
Tagging subscribers to this area: @dotnet/area-system-componentmodel Issue DetailsBackground and motivationConverting between I was expecting this to work, but converting from string actually throws:
TypeConverter dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
DateOnly? date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?; Having built-in support for type conversion would probably help to drive adoption of the relatively new Addressing this issue would fix #59253 which is mentioned in System.Runtime work planned for .NET 7 (but only under the Backlog list, not the Planned for .NET 7 list). Note that a similar proposal for the API Proposalpublic class DateOnlyConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType);
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType);
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value);
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType);
public override bool IsValid(System.ComponentModel.ITypeDescriptorContext? context, object? value);
} API UsageUsing the converter would most likely happen through var dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
var date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?; or even through a higher level abstractions such as Microsoft.Extensions.Configuration.Binder (which also uses // Let's make the end of the world date configurable because we're not really sure about that date
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { ["EndOfWorldDate"] = "2012-12-21" })
.Build();
var endOfTheWorldDate = configuration.GetValue<DateOnly>("EndOfWorldDate"); It could also be used by directly creating a var dateOnlyConverter = new System.ComponentModel.DateOnlyConverter();
var date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?; Alternative DesignsTo the best of my knowledge, there's no alternative design that would make sense for adding a new Risks
|
Could you please merge this proposal with the one in #68744? there is no need to track them in two different proposals. Thanks for filing the proposal. |
This would be very useful. Many components and libraries use In the meantime, it's possible to make it work by adding the converter dynamically at runtime: class DateOnlyConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string s)
return DateOnly.Parse(s, culture);
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ((DateOnly)value).ToString(culture);
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value)
{
if (value is DateOnly)
return true;
return base.IsValid(context, value);
}
}
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyConverter))); |
@thomaslevesque that is right. I believe the ask is to avoid having different users implement the same converters to make their scenarios work. |
@0xced I am noticing some differences comparing the proposal to like DateTimeConverter. Any reason for that? you are proposing to have the extra API |
@0xced I have updated the APIs in the proposal. Let's me know if you have any concern. |
We added System.Half, Int128, and UInt128 to the list to get caught up on primitive-ish types in the TypeConverter space. (We didn't think there was enough of a scenario for NFloat, NInt, or NUInt). namespace System.ComponentModel
{
public class DateOnlyConverter : System.ComponentModel.TypeConverter
{
public DateOnlyConverter() { }
}
public class TimeOnlyConverter : System.ComponentModel.TypeConverter
{
public TimeOnlyConverter() { }
}
public class HalfConverter : System.ComponentModel.BaseNumberConverter
{
public HalfConverter() { }
}
public class Int128Converter : System.ComponentModel.BaseNumberConverter
{
public Int128Converter() { }
}
public class UInt128Converter : System.ComponentModel.BaseNumberConverter
{
public UInt128Converter() { }
}
} |
Thank you for implementing this, Tarek and Jeremy! |
Background and motivation
Converting between
System.DateOnly
orSystem.TimeOnly
andstring
is currently not supported using aSystem.ComponentModel.TypeConverter
. Many system types are supported out of the box (System.DateTimeOffset
,System.Guid
,System.TimeSpan
,System.Uri
etc.) so I thinkSystem.DateOnly
andSystem.TimeOnly
would be welcome additions.I was expecting this to work, but converting from string actually throws:
Having built-in support for type conversion would probably help to drive adoption of the relatively new
DateOnly
andTimeOnly
type.Addressing this issue would fix #59253 which is mentioned in System.Runtime work planned for .NET 7 (but only under the Backlog list, not the Planned for .NET 7 list).
API Proposal
API Usage
Using the converter would most likely happen through
TypeDescriptor.GetConverter(typeof(…))
:or even through a higher level abstractions such as Microsoft.Extensions.Configuration.Binder (which also uses
TypeDescriptor.GetConverter
internally) as described in #64739:It could also be used by directly creating a
DateOnlyConverter
orTimeOnlyConverter
instance but this is a less likely scenario.Alternative Designs
To the best of my knowledge, there's no alternative design that would make sense for adding new
System.ComponentModel.TypeConverter
subclasses.Risks
System.ComponentModel.DateOnlyConverter
andSystem.ComponentModel.TimeOnlyConverter
being a new classes, no risks are introduced.The text was updated successfully, but these errors were encountered: