c# - DateTime TypeConverter -


I have the code below to convert the string to type T. It works fine for all other types but gives an error T typetime type.

  TypeConverter c = TypeDescriptor.GetConverter (typef (t)); Return (t) c.ConvertTo (obj, typeof (T))  

I pass like a string

  obj = "09/09/2009 " 

This throws error." {DateTimeConverter} is unable to convert 'System.String' to 'System.DateTime'. "}

If you know that you are getting a string, you can use TypeConverter.ConvertFromString instead Are there. It works with DateTimeConverter , although I do not know why ConvertTo is not.

For example, this works:

  TypeConverter c = TypeDescriptor.GetConverter (typefime); Console.lightline ((date time) c.ConvertFromString ("09/09/2009"));  

Alternatively, just works as ConvertFrom :

  TypeConverter c = TypeDescriptor.GetConverter (typef (Datetime)); Console.lightline ((datetime) c.ConvertFrom ("09/09/2009"));  

It is going to change from to a date time because it is like a converter.

You should however be careful about cultural issues.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -