.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Here's one to scratch your head over
 

Here's one to scratch your head over

int n = 1;
var converter = TypeDescriptor.GetConverter(n.GetType());
var o = converter.ConvertFrom(n);

NotSupportedException was unhandled:
"Int32Converter cannot convert from System.Int32."
Tergiver
Hmya, TypeDescriptor and TypeConverter are there to convert back-and-forth from human readable strings in PropertyGrid to a binary type. Int32Converter implements FromString and ToString. Converting from int to int invokes the "what the heck are you doing" exception. These are not general-purpose classes, just helpers to get a property grid going. WPF has problems like that too, makes everybody think a collection should always be Observable.

More dots in the namespace name might have advisable. More dots indicates more specialized behavior. Although there are definitely too many dots in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter. API design can be tricky...

Hans Passant.
nobugz
I think its funny. It's like some weird blender that can take some salt, Tequila, Triple Sec, ice, a lime and whip up a perfect Margarita, but it can't take a lime and make lime juice.
Tergiver
When you are doing something unusual and unnecessary, you are probably not doing it on purpose. Better to flag unintentional errors than to allow wasting some CPU cycles.


Please mark the post answered your question as the answer, and mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
Visual C++ MVP
Sheng Jiang 蒋晟
It is not a blender, it is brilliant API design. Another example is BitConverter.GetBytes(). Has overrides for all the common value types, except GetBytes(Byte). It would have been trivial to add that overload too. That they intentionally left that out is awesome. It is up to you to discover the awsomeness in TypeConverter, can't really help you with that.

Hans Passant.
nobugz

> It is up to you to discover the awsomeness in TypeConverter

nobugz,

It's in the System.ComponentModel namespace. Lots of clever stuff in there.However (along with System.Design), I consider it one of the most perplexing namespaces in all of .NET. It is very abstract.

BinaryCoder
Agreed. Although I think the problem is caused by under-documentation. I never could get anywhere with those namespaces without using Reflector. My personal nemesis is System.Configuration, hit the wall every time on that one. Either I got the wrong mental model or it is just plain old poorly designed. Writing a SettingsProvider is ridiculously difficult.
Hans Passant.
nobugz

You can use google to search for other answers

Custom Search

More Threads

• ElementCollection in 2.0
• Reg. Error 25007. Reply immediately if you know.
• Getting rid of control characters in the code
• Accessing OLE2 streams
• How to make TraceSource print DateTimes in local time instead of GMT?
• Help with WMI
• How to veto suspend and shutdown request using C#?
• Why does this compile?
• Store DataTable in custom config section
• Euro encoding in UTF8