Skip to content

Instantly share code, notes, and snippets.

View margusmartsepp's full-sized avatar
😇
It is that simple

Margus Martsepp margusmartsepp

😇
It is that simple
  • Gunvor SA
  • Tallinn
View GitHub Profile
public static bool Deserialize<T>(this String str, out T item)
{
item = default(T);
try
{
using (var reader = XmlReader.Create(new StringReader(str)))
{
item = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
return true;
@margusmartsepp
margusmartsepp / gist:ef74a3ed27c2ad67f05f
Created September 10, 2014 10:28
Changing default value for inherited members
public class Class1
{
internal static int Myvar;
private int _myVar;
public int MyVar1
{
get{return this.GetType().IsSubclassOf(typeof(Class1)) ? Myvar : _myVar;}
set{_myVar = value;Myvar = value;}
}
}
@margusmartsepp
margusmartsepp / gist:2643e248f5bc46450243
Created September 11, 2014 07:10
DataTable to json
//add reference System.Data
//add reference System.Web.extensions
//add reference System.Web.DataTableExtensions
public static string ConvertToJson(DataTable dt, int page = 0, int count = 100)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var rows = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.AsEnumerable().Skip(page * count).Take(count).ToList())
{
rows.Add(dt.Columns.Cast<DataColumn>().ToDictionary(col => col.ColumnName, col => dr[col]));
@margusmartsepp
margusmartsepp / gist:bf9b258edc4bca442cc9
Last active August 29, 2015 14:06
String extension Diff
public static class Extensions
{
public static string Diff(this string first, string second)
{
var s1 = first.ToCharArray();
var s2 = second.ToCharArray();
int s1P = s1.Length, s2P = s2.Length;
var num = new int[s1P + 1, s2P + 1];
// fill array
for (var i = 1; i <= s1P; i++)
@margusmartsepp
margusmartsepp / gist:07498cd0233884a2810b
Created September 16, 2014 10:32
Datetime diff in days, months and years
public static class Extensions
{
private const double DaysInYear = 365.242;
private const double DaysInMonth = 30.4368;
public static int GetDays(this TimeSpan ts)
{
return (int)((ts.TotalDays % DaysInYear % DaysInMonth));
}
public static int GetMonths(this TimeSpan ts)
@margusmartsepp
margusmartsepp / gist:a1fd8b1940bdbed9a106
Created September 16, 2014 10:50
Win 7 glass background
[DllImport("gdi32")]
private static extern IntPtr CreateEllipticRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("dwmapi")]
private static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DwmBlurbehind pBlurBehind);
public struct DwmBlurbehind
{
public int DwFlags;
public bool FEnable;
public IntPtr HRgnBlur;
public bool FTransitionOnMaximized;
@margusmartsepp
margusmartsepp / gist:db0d1fba80c6fc4944f9
Created September 16, 2014 10:50
String DeserializeJson
public static class Extensions
{
public static bool DeserializeJson<T>(this String str, out T item)
{
item = default(T);
try
{
item = new JavaScriptSerializer().Deserialize<T>(str);
return true;
}
@margusmartsepp
margusmartsepp / gist:d9bce523a19d0475017f
Last active June 24, 2018 00:19
C# Dictionary push and pull
public static class Extensions
{
public static bool Push<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
try
{
if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
@margusmartsepp
margusmartsepp / gist:73685649ecd546a436ab
Created September 16, 2014 10:55
C# call a instance method
class MyClass
{
static Dictionary<string, Func<MyClass, string, bool>> commands
= new Dictionary<string, Func<MyClass, string, bool>>
{
{ "Foo", (@this, x) => @this.Foo(x) },
{ "Bar", (@this, y) => @this.Bar(y) }
};
public bool Execute(string command, string value)
@margusmartsepp
margusmartsepp / gist:0617df9345ea7a37b5b0
Created September 17, 2014 18:24
NUnit tests comparing sync, async, lazy
//PM> Install-Package Rx-Linq
readonly List<string> _list = new List<string> { "http://www.google.com", "https://www.gmail.com", "http://www.aripaev.ee" };
private readonly string format = "[{0}] {1} : {2} [{3}]";
[Category("WebClient")]
[TestCase("sync" )]
public void SynchronousTest(string name)
{
DateTime start = DateTime.Now;
var dict = _list.ToDictionary(o => o, o => new WebClient().DownloadString(new Uri(o)));
dict.Keys.ToList().ForEach(o => Console.WriteLine(format, DateTime.Now - start, o, dict[o].Length, name));