Posted By

wallybh on 08/31/10


Tagged

type generics c conversion


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Tyster
Priestd09


C# Generic Type Conversion


 / Published in: C#
 

Enables to convert types using generics. Simple and clean.

  1. public class GenericConverter
  2. {
  3. public static T Parse<T>(string sourceValue) where T : IConvertible
  4. {
  5. return (T)Convert.ChangeType(sourceValue, typeof(T));
  6. }
  7.  
  8. public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible
  9. {
  10. return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
  11. }
  12. }

Report this snippet  

You need to login to post a comment.