Can access to Parse methods for a given type based on an instance of Type in C #?

I use DataTableand assign columns of different types. I have a scenario in which I receive data Stringand I want to analyze it based on the assigned type column, but I cannot figure out how to get to the analysis methods.

Is it possible to share type parsing methods?

+5
source share
2 answers

You are looking for Convert.ChangeType.

+5
source

If you are using something larger than the base types (which Convert.ChangeTypehandles pretty nicely), the preferred way to do this is through TypeConverter:

var converter = TypeDescriptor.GetConverter(type);
object val = converter.ConvertFromString(s); // note various overloads,
                                             // or ConvertFromInvariantString

, ( ), ( [TypeConverter(...)]), (TypeDescriptor.AddAttributes(...)).

+2

All Articles