C# Generic Type Conversion


/ Published in: C#
Save to your folder(s)

Enables to convert types using generics. Simple and clean.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.