The Opposite of IFormattable

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

Dear C# Sages,

For my own persistence purposes I need to be able to load an object from
some source and re-fit it with its value that's represented as a string.

Many objects support "Parse" which takes a string input and sets the value
for the object. However, I can't find a way to "pre-identify" those objects
with the same ease I can do the opposite: ask an object if it's IFormattable
so that I can invoke its "ToString" method. Am I doomed to use reflection to
actually ask each object i come across if it has a "Parse" method that has a
string as paramter? Isn't there some "uber-Interface" this adheres to?

am I missing something?

Thanks
- Ron
 
You can *always* invoke the ToString method, which is a method of the base
class Object.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
well, not always: it wont always give me a "high fidelity" string I can
persist and then re-load. (Image objects for instance).

But in any case, I was talking about the other way around, a
"FromString" - -- the "Parse" method which doesn't seem to come from an
interface but rather embedded in various classes which IMHO is a design
oversight by MS.

Ron
 
Ron,

If you build a new class, should the ToString method always be declared
overridden from object in a method (what is not always the fact and than you
get back only object information)

If it is overloaded can be dependable from the class, when it is overloaded
that can be done with everything. In the String class one of those has the
IFormatProvider, but how it works is completely dependend from how the
builder of the class has used the overriding and possible the overloading.

In normal OOP programming there should in my opinion be no reason to use
late binding, therefore no reason to find out what members an object has and
how they are implemented with reflection.

Cor
 
What are your requirements? Most classes dont' translate from strings well,
if at all. What sort of string would resolve to a Socket instance, for
example? There are very few types, and most of them are Value Types, that
can be parsed from strings. Other than that, you might benefit from using
XML Serialization, again, according to your requirements.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
Back
Top