Last active
December 12, 2015 10:39
-
-
Save fbstj/4761153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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