Initial commit

This commit is contained in:
2014-04-30 16:57:42 -04:00
commit 5c4a1c8b4c
126 changed files with 52769 additions and 0 deletions

45
Extensions/Extensions.cs Normal file
View File

@@ -0,0 +1,45 @@
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace Common.Extensions
{
public static class Extensions
{
public static bool GetBitValue(this int integer, int bit)
{
return (integer & (1 << bit)) != 0;
}
public static int SetBitValue(this int integer, int bit, bool value)
{
if (value)
integer |= 1 << bit;
else
integer &= ~(1 << bit);
return integer;
}
public static string ToJson<T>(this T obj) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, obj);
return Encoding.Default.GetString(stream.ToArray());
}
}
public static Task<T> WithTimeout<T>(this Task<T> task, int duration)
{
return Task.Factory.StartNew(() =>
{
bool b = task.Wait(duration);
if (b) return task.Result;
return default(T);
});
}
}
}