Skip to content

Instantly share code, notes, and snippets.

View xanathar's full-sized avatar
🐢
I may be slow to respond. Who am I kidding?. I will be VERY slow to respond.

Marco Mastropaolo xanathar

🐢
I may be slow to respond. Who am I kidding?. I will be VERY slow to respond.
View GitHub Profile
@xanathar
xanathar / OpenFileDialog - SaveFileDialog
Created June 3, 2014 16:49
Quick reminder of WinForms standard file dialogs
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save on XML";
sfd.OverwritePrompt = true;
sfd.DefaultExt = "xml";
sfd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
...
@xanathar
xanathar / RegEx-NamedCapture
Created June 3, 2014 16:52
Example of regex w/named capture & lazy static initialization
const string REGEX = @"((?<id>\d+)\:)?(?<par1>\d+)\-(?<par2>\d+)\-(?<par3>\d+)(\/(?<par4>\d+)\-(?<par5>\d+))?";
private static Lazy<Regex> g_RegEx = new Lazy<Regex>(() => new Regex(REGEX,
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.CultureInvariant));
...
Match m = g_RegEx.Value.Match(value);
Group gID = m.Groups["id"] as Group;
@xanathar
xanathar / HttpListener #1
Created June 17, 2014 12:22
HttpListener #1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Collections.Specialized;
@xanathar
xanathar / HttpListener #2
Created June 17, 2014 12:29
HttpListener #2 - WCF / MVC way
public class CommandsController : ApiController
{
[HttpGet][HttpPost][HttpDelete]
public void Action1()
{
}
[HttpGet]
[HttpPost]
[HttpDelete]
@xanathar
xanathar / LockHelper
Last active August 29, 2015 14:02
Wrapper of Monitor class for easier deadlock debug
// original source is likely to be https://github.com/thinkpixellab/bot/blob/master/net40-client/Core/LockHelper.cs although I can't verify it anymore.
/// <summary>
/// Provides services of Monitor static class, but while allowing better debugging of deadlocks.
/// </summary>
public class LockHelper
{
/// <summary>
/// Creates a new instance of LockHelper.
@xanathar
xanathar / ExpTree_PropertyAccessors
Created September 11, 2014 14:34
Get/Set instance property using expression trees dynamic code
// This builds untyped accessors. Remove converts for strong typing.
var paramExp = Expression.Parameter(typeof(object), "obj");
var castParamExp = Expression.Convert(paramExp, this.UserDataDescriptor.Type);
var propAccess = Expression.Property(castParamExp, PropertyInfo.Name);
var castPropAccess = Expression.Convert(propAccess, typeof(object));
var lambda = Expression.Lambda<Func<object, object>>(castPropAccess, paramExp);
Func<object, object> getter = lambda.Compile();
@xanathar
xanathar / gist:6954d46af5097218a7fe
Created September 16, 2014 09:53
Image resize w/System.Drawing
// Taken from http://social.msdn.microsoft.com/Forums/vstudio/en-US/a43fe625-a5cd-4438-895f-3ddeec6eb866/image-resizing-using-c?forum=csharpgeneral
public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
@xanathar
xanathar / EventHandlers in MoonSharp
Created December 12, 2014 20:44
Sample of event handlers in MoonSharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MoonSharp.Interpreter;
namespace PerformanceComparison
{
class Sample
@xanathar
xanathar / MoonSharp_UserData_Runtime_Extension
Created March 31, 2015 15:52
Extend MoonSharp userdata at script runtime
public class MyObject
{
public int GetSomething()
{
return 10;
}
}
[Test]
public void MetatableExtensibleObjectSample()
@xanathar
xanathar / MoonSharpInfiniteLoopCheck
Created April 13, 2015 08:36
[MoonSharp] - How to limit execution of a script to n instructions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Debugging;
namespace Tutorials.Chapters
{