Skip to content

Instantly share code, notes, and snippets.

@fbstj fbstj/ext.cs
Last active Dec 12, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.