C# Generic Type Conversion

by UnquaLe 25. October 2007 02:45

I thought C# Generics are useful also for type convertion. I messed code alittle and the emergent code snippet looks fine for me. It is easy and simple. 

public class GenericConverter
{
    public static T Parse<T>(string sourceValue) where T : IConvertible
    {
      return (T)Convert.ChangeType(sourceValue, typeof(T));
    }

    public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible
    {
      return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
    }
}

//Usage
DateTime datetime = GenericConverter.Parse<DateTime>("12.10.2007");


IFormatProvider providerEN = new System.Globalization.CultureInfo("en-US", true);
IFormatProvider providerTR = new System.Globalization.CultureInfo("tr-TR", true);

DateTime x = GenericConverter.Parse<DateTime>("12.10.2007", providerTR); // ddMMyyyy
DateTime y = GenericConverter.Parse<DateTime>("12.10.2007", providerEN); // MMddyyyy

int iValue = GenericConverter.Parse<int>("12");


Enjoy !.

Doga. 

Tags:

Comments

10/25/2007 8:22:09 AM #

trackback

Trackback from DotNetKicks.com

C# Generic Type Conversion

DotNetKicks.com | Reply

8/28/2010 8:14:51 PM #

oil paintings

I am not really sure if best practices have emerged around things like that, but I am sure that your great job is clearly identified. I was wondering if you offer any subscription to your RSS feeds as I would be very interested and can?t find any link to subscribe here.

oil paintings United States | Reply

10/25/2007 7:11:56 PM #

Gokhan Demir

Hey man, this was a great idea! Nicely done!

Gokhan Demir Turkey | Reply

10/26/2007 12:06:26 AM #

Garry Shutler

I don't understand the point of this. How is this better than DateTime.Parse, Integer.Parse or creating a static method on whatever class you wish to convert to?

Garry Shutler United Kingdom | Reply

2/18/2008 6:20:55 AM #

Tuna Toksoz

Nullable Type Conversion


  public static class TConverter
  {
    public static T ChangeType<T>(object value)
    {
      return (T)ChangeType(typeof(T),value);
    }
    public static object ChangeType(Type t,object value)
    {
      TypeConverter tc = TypeDescriptor.GetConverter(t);
      return tc.ConvertFrom(value);
    }
    public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
    {

      TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));

    }

  }


this seems to be a better solution. Also works for nullables.

Tuna Toksoz Turkey | Reply

8/30/2010 4:01:27 PM #

girls cam

Pretty good post. I just came across your site and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

girls cam United States | Reply

8/30/2010 6:52:43 PM #

Caron Nordberg

I´ve been reading your blog for awhile and also it certainly not occurred to me to comment. That is entirely ironic, because I´ve spent a lot of time over the past few months studying what it takes to make people comment on my own website. Immediately after reading a few your posts I guess it´s controversial topics that stir people´s emotions to the point where they can´t just ´let it go.´

Caron Nordberg United States | Reply

8/31/2010 1:03:34 AM #

Trading Computers

Industry Leading Trading Computers will help make traders more money...

Trading Computers United States | Reply

8/31/2010 6:58:35 AM #

Latanya Baley

All in a days work. all just beginning actually.  <a href=”http://www.ebulletproofvest.com”>bullet vest</a>

Latanya Baley United States | Reply

8/31/2010 8:36:37 PM #

Replica Rolex Watches

A good blog, I like! Welcome to our website to find your favorite watch, click on my name! Look forward to your visit! Thank you!

Replica Rolex Watches United States | Reply

9/2/2010 3:29:58 AM #

Accounting Software Download

Thought-provoking, this is exactly ideal.  Looking forward to more.  

Accounting Software Download United States | Reply

9/2/2010 10:32:34 AM #

Mozelle Euresti

great post! i'm bookmarking this!

Mozelle Euresti United States | Reply

9/2/2010 7:41:29 PM #

Capacitor for 250W MH - Floralux, 15uf/400v

I really love to read articles but when i read yours i am more interested with it.
I am hoping the same best work from you in the future as well.
your doing excellent.
just keep up the good work.

Capacitor for 250W MH - Floralux, 15uf/400v United States | Reply

9/3/2010 7:01:36 AM #

dickies messenger bag

Very well written blog and a big thanks for sharing such a wealth of information with the community as a whole.  This is just the type of information I need.

dickies messenger bag United States | Reply

9/3/2010 8:52:39 AM #

military style jacket

Great post and great information.  Glad I found your site as i'm sure it will help me in clear up some issues i'm havingin development.

military style jacket United States | Reply

9/3/2010 1:45:05 PM #

Sex Cam

Great nightime reading thankyou.

Sex Cam United States | Reply

9/3/2010 6:22:54 PM #

Job - MexicoFishingDeals

This is a nice website!, i just recently found it and now is on my Bookmarks.
Keep the effort of keeping this Blog updated, and if you have a chance please visit my site http://www.mexicofishingdeals.com
thanks and keep tight lines.
Job O.

Job - MexicoFishingDeals United States | Reply

9/4/2010 4:12:28 AM #

Black Hat SEO Forum

Can you post more about this please if possible and let me know by emailing with subject ?

Black Hat SEO Forum India | Reply

9/4/2010 2:50:40 PM #

urbantrendyclothing

<A href=" www.urbantrendyclothing.com/">wholesale gucci clothing</A>
<A href=" http://www.urbantrendyclothing.com/">cheap jordan sneakers</A>  
<A href=" http://www.urbantrendyclothing.com/">cheap prada clothing</A>  
<A href=" http://www.urbantrendyclothing.com/">cheap polo belts</A>
<A href=" www.urbantrendyclothing.com/">wholesale fendi shoes</A>
<A href=" http://www.urbantrendyclothing.com/">urban coogi suits</A>
<A href=" http://www.urbantrendyclothing.com/">nike jordan shoes</A>
<A href=" http://www.urbantrendyclothing.com/">cheap gucci clothing</A>
<A href=" www.urbantrendyclothing.com/">wholesale fendi handbags</A>
<A href=" www.urbantrendyclothing.com/">replica gucci shoes</A>

urbantrendyclothing People's Republic of China | Reply

9/5/2010 4:01:38 PM #

spyware removal software

You should wite a blog post about a suggestion of blogs for people like me. I came across your blog last week when I was researching, unfortuneatly I am not really into the blog thing. I don't think it's because I do not like blogs, but more than likely due to the fact that I'm slightly slow to them.  

spyware removal software United States | Reply

9/6/2010 12:09:09 AM #

cheap nike sportswear

What doesn't kill us makes us stronger.

cheap nike sportswear United States | Reply

9/6/2010 11:19:43 PM #

web development firm


I love being at the center of attention and it is so lovely to get it on the wen and share your exclusive thoughts
  

web development firm United States | Reply

9/7/2010 3:19:02 AM #

Brain Exercises

Eeek!  Love your blog.

Brain Exercises United States | Reply

9/7/2010 3:52:44 AM #

Aishwarya Sen

This is pretty interesting ;), i was looking for something but found your site instead through Google . I love blogging. Anyways, just wanted to drop by and say hi ;). i have subscribed to your site and i am looking forward to the latest stuffs , Thanks

Aishwarya Sen United States | Reply

9/7/2010 4:09:54 AM #

Jerome Pennick

Whenever I visit this blog, there's always something interesting to read. Great blog!

Jerome Pennick United States | Reply

9/7/2010 7:47:50 AM #

air jordans

This blog keeps me coming back. I love your work.

air jordans United States | Reply

9/7/2010 12:14:32 PM #

Martial Arts X

I am tired of reading spammy comments. You should seriously find a way to get rid of this. That said, I think this is a good post. I dont know much about it, but I am dedicated to martial arts. Its more that just fighting; its a way of life. Its my LIFE Laughing

Martial Arts X United States | Reply

9/7/2010 2:29:46 PM #

cheapretailjordan

<A href=" http://www.cheapretailjordan.com/">cheap gucci clothing</A>
<A href=" http://www.cheapretailjordan.com/">Juicy Couture Bikini</A>
<A href=" http://www.cheapretailjordan.com/">Chanel t shirts</A>
<A href=" www.cheapretailjordan.com/">Christian Audigier Jeans</A>
<A href=" http://www.cheapretailjordan.com/">Polo t shirt</A>
<A href=" http://www.cheapretailjordan.com/">ed hardy handbags</A>
<A href=" http://www.cheapretailjordan.com/">armani jeans</A>
<A href=" http://www.cheapretailjordan.com/">Coogi Clothing</A>
<A href=" http://www.cheapretailjordan.com/">Radii ShoeS</A>
<A href=" http://www.cheapretailjordan.com/"> cheap dolce gabbana shoes</A>

cheapretailjordan People's Republic of China | Reply

9/8/2010 3:16:25 AM #

Web Based Accounting Software

Hola.  I really like your blog.  Want to exchange links?

Web Based Accounting Software United States | Reply

9/8/2010 5:56:37 AM #

SEO Services India

Wow, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate a lot and never seem to get something done.

SEO Services India United States | Reply

9/8/2010 9:53:34 AM #

slimming undergarments

You possess some extremely well prepared subject matter and to finish it it's very helpful as well.  Many thanks for posting this type of valuable and insightful written content with ordinary people!

slimming undergarments United States | Reply

9/10/2010 1:46:39 AM #

Prada Shoes

Thanks for the great post, it is appreciated. I will follow your blog.

Prada Shoes United States | Reply

9/10/2010 2:32:53 AM #

Swimming Tips

Greeting.  I really like your blog.  Mind if I link to it?

Swimming Tips United States | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading