-
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.TimeOnlyConverter class #68744
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 timeOnlyConverter = TypeDescriptor.GetConverter(typeof(TimeOnly));
TimeOnly? time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?; 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 TimeOnlyConverter : 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 timeOnlyConverter = TypeDescriptor.GetConverter(typeof(TimeOnly));
var time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?; or even through a higher level abstractions such as Microsoft.Extensions.Configuration.Binder (which also uses var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { ["BreakfastTime"] = "7:00:00" })
.Build();
var breakfastTime = configuration.GetValue<TimeOnly>("BreakfastTime"); It could also be used by directly creating a var timeOnlyConverter = new System.ComponentModel.TimeOnlyConverter();
var time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?; Alternative DesignsTo the best of my knowledge, there's no alternative design that would make sense for adding a new Risks
|
I have just amended #68743 to cover both |
Background and motivation
Converting between
System.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.TimeOnly
would be a welcome addition.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
TimeOnly
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).
Note that a similar proposal for the
System.DateOnly
type has been filed as #68743.API Proposal
API Usage
Using the converter would most likely happen through
TypeDescriptor.GetConverter(typeof(TimeOnly))
: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
TimeOnlyConverter
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 a new
System.ComponentModel.TypeConverter
.Risks
System.ComponentModel.TimeOnlyConverter
being a new class, no risks are introduced.The text was updated successfully, but these errors were encountered: