Is it possible to do a reverse type lookup on string.format formatting strings. Say for example I have {0:0.00} formatting string. It’s used to format a decimal. Can I pass it to a function and have it return the type in this case decimal. Or pass {0:y yy yyy yy} and have it return DateTime?
Along the lines of this (Can I check if a format specifier is valid for a given data type?) but for String.Format and I can’t pass in the type I’m looking for. I guess I can have a list of types to try against.
Any ideas?
EDIT:
I’m reading data from database that always comes in as string. I don’t know its type. I want to fit it into some other database where I do know what type it’s supposed to be. The way I know the type is because somebody has provided a string.format parameter. They don’t provide a type just the string.format parameter. So I was trying to see if I can get the type from the formatting provided. I realize this is a bad design but that’s what I’m forced into.
I have never come across anything like this, nor would it be possible in the absolute. Some format strings will work for more than one type. I am going to say there is nothing out of the box to do this.
If you look here most of the numeric formats will work for any numeric type, int, float, double, decimal… A few will as you point out only work with dates, but it would be difficult to design a good API for describing that.
As the commentators have said here if you post a question that more broadly reflects what you are trying to achieve you may get some better ideas.