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

Proposal: new System.ComponentModel.EncodingConverter class #27489

Open
0xced opened this issue Sep 27, 2018 · 1 comment
Open

Proposal: new System.ComponentModel.EncodingConverter class #27489

0xced opened this issue Sep 27, 2018 · 1 comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.ComponentModel
Milestone

Comments

@0xced
Copy link
Contributor

0xced commented Sep 27, 2018

Rationale and Usage

Converting between System.Text.Encoding and string or int is currently not supported using a System.ComponentModel.TypeConverter. Many system types are supported out of the box (System.DateTimeOffset, System.Guid, System.TimeSpan, System.Uri, System.Version etc.) so I think System.Text.Encoding would be a welcome addition.

I was expecting this to work, but converting from string or int actually throws a NotSupportedException.

using System.ComponentModel;
using System.Diagnostics;
using System.Text;
TypeConverter encodingConverter = TypeDescriptor.GetConverter(typeof(Encoding));
Encoding utf8EncodingFromString = encodingConverter.ConvertFromString("utf-8") as Encoding;
Encoding utf8EncodingFromInt = encodingConverter.ConvertFrom(65001) as Encoding;

Proposed API

public partial class EncodingConverter : System.ComponentModel.TypeConverter
{
    public EncodingConverter();
    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);
}

Details

Converting from string or int to System.Text.Encoding is straightforward using Encoding.GetEncoding, implementation would be very similar to the VersionConverter pull request (dotnet/corefx#28516) merged about one month ago.

Open Questions

  • Should converting from int (interpreted as a code page) be supported ? I started writing unit tests and noticed a lot of edge cases:
    • If supporting conversion from int, should we also support conversion from all types implicitly convertible to int, i.e. sbyte, byte, short, ushort and char ? (Yes in my opinion)
    • If supporting conversion from int, should we support conversion from larger types, such as long when the value fits an int, e.g. long codePage = 1200. (Yes in my opinion)
    • If supporting conversion from int, should we support conversion int as string, e.g. string codePage = "1200" ? (This is where I started to wonder if supporting conversion from int was a good idea at all)

Pull Request

A pull request with the proposed changes is available: dotnet/corefx#32527

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Jul 1, 2020
@ericstj ericstj modified the milestones: 5.0.0, Future Jul 1, 2020
@ericstj
Copy link
Member

ericstj commented Jul 1, 2020

There's nothing blocking you from creating this yourself today outside the framework. We'd need to see some significant use cases our community demand to add this to the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.ComponentModel
Projects
None yet
Development

No branches or pull requests

4 participants