Skip to content

Instantly share code, notes, and snippets.

View sandrock's full-sized avatar

SandRock sandrock

View GitHub Profile
@sandrock
sandrock / Extensions.InvariantString.cs
Last active February 7, 2025 00:08
C# Returns an invariant, roundtrip-safe string representation numeric values
namespace System
{
using System.Globalization;
// source: https://gist.github.com/sandrock/6fe3298b8ac6d9d0d9872dd811a63908
/// <summary>
/// Extension methods for common types that provide ToInvariantString() capability.
/// </summary>
@sandrock
sandrock / ApiController.cs
Created November 6, 2019 10:59
ASP WebAPi better ApiController NotFound method with negociated response content
namespace MyWebApi
{
public abstract class ApiController : System.Web.Http.ApiController
{
/// <summary>
/// Returns a NotFound HTTP result (404) with a negotiated full-bodyed result.
/// </summary>
/// <returns></returns>
protected override System.Web.Http.Results.NotFoundResult NotFound()
@sandrock
sandrock / ConvertEx.cs
Last active February 7, 2025 00:08
C# convert a hexadecimal string to a byte array (tolerant to extra characters)
namespace Home
{
using System;
using System.Collections.Generic;
public static class ConvertEx
{
/// <summary>Reads a hex string into bytes</summary>
public static IEnumerable<byte> HexadecimalStringToBytes(string hex)
@sandrock
sandrock / DisplayFileSize.cs
Last active March 25, 2020 11:44
ASP MVC Html.DisplayFileSize helper
namespace WhateverNamespaceYouWant
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
@sandrock
sandrock / EmailAddress.cs
Last active December 18, 2015 12:12
Launch email client from WinRT
namespace MyApp
{
using System;
using System.Collections.Generic;
using System.Linq;
public class EmailAddress
{
public EmailAddress()
{
@sandrock
sandrock / gist:7aed3f40ad3b00c28560
Last active August 29, 2015 14:23
Missing stuff in WinRT

Missing stuff in WinRT

Bindings

BindingOperations.GetBinding() [0]

BindingOperations.ClearBinding()

@sandrock
sandrock / gist:0faf8dba52963f1f41ae
Created December 13, 2014 23:07
HttpWebResponse from WebException
public class Foo{
public void Bar() {
var request = (HttpWebRequest)HttpWebRequest.Create(context.UrlPath);
// ........
// get response
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
@sandrock
sandrock / FillParentBehavior.cs
Created April 11, 2013 14:37
Helps resize a control to fill its parent. Works only with a Image control but may be enhanced to work with other controls. Works in Windows Phone, may work in Silverlight/WPF. See http://stackoverflow.com/questions/13535022/windows-8-image-uniformfill-centered
namespace MyPhoneApp.Behaviors
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@sandrock
sandrock / StringTransformer.AddHtmlLineBreaks
Created September 6, 2012 08:29
Inserts HTML line breaks (<br />) before all newlines
public static class StringTransformer
{
/// <summary>
/// Inserts HTML line breaks (&lt;br /&gt;) before all newlines.
/// </summary>
/// <param name="text">text containing lines</param>
/// <returns></returns>
public static string AddHtmlLineBreaks(this string text)
{