Skip to content

Instantly share code, notes, and snippets.

@fbstj
Last active December 12, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbstj/4761153 to your computer and use it in GitHub Desktop.
Save fbstj/4761153 to your computer and use it in GitHub Desktop.
using System;
using System.IO.Ports;
using System.Threading;
using System.Globalization;
namespace Terminal
{
static class Program
{
static void Main(string[] s)
{
while (true)
{
var t = Console.ReadLine();
int i;
double j;
if (t.To(out i))
Console.WriteLine("int: " + i);
if (t.To(out j))
Console.WriteLine("float: " + j);
}
}
public static bool To<T>(this string value, out T result)
{
result = default(T);
var convertor = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
if (convertor == null || !convertor.IsValid(value))
{
return false;
}
result = (T)convertor.ConvertFrom(value);
return true;
}
public static T With<T>(this T self, Func<T> action)
{
action(self);
return self;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment