Skip to content

Instantly share code, notes, and snippets.

@kpol
kpol / ValidateJsonAntiForgeryTokenAttribute.cs
Created August 2, 2012 00:39
ASP.NET MVC 2 -- ValidateAntiForgeryTokenAttribute for JSON requests
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ValidateJsonAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
@kpol
kpol / gist:3752921
Created September 19, 2012 23:06
CombineHashCodes
public static int CombineHashCodes(params int[] hashCodes)
{
if (hashCodes == null)
{
throw new ArgumentNullException("hashCodes");
}
if (hashCodes.Length == 0)
{
throw new IndexOutOfRangeException();
@kpol
kpol / TagGeneratorPlugin.cs
Created October 2, 2012 23:51
SpecFlow custom generator plugin
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using BoDi;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Generator.Plugins;
using TechTalk.SpecFlow.Generator.UnitTestConverter;
using TechTalk.SpecFlow.Generator.UnitTestProvider;
@kpol
kpol / EnumerableExtensions.cs
Last active February 6, 2018 22:14
Enumerable Extensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace LinqExtensions
{
public static class EnumerableExtensions
{
/// <summary>
/// <summary>
/// Gets value from DataReader. Handles <see cref="DBNull"/>.
/// </summary>
/// <typeparam name="T">Any type including <see cref="Nullable{T}"/> values.</typeparam>
/// <param name="reader"></param>
/// <param name="columnName"></param>
/// <returns></returns>
public static T GetValue<T>(this IDataReader reader, string columnName)
{
var value = reader[columnName];
@kpol
kpol / BinarySearchTree.cs
Last active February 11, 2018 22:49
BinarySearchTree C# implementation
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication10
{
public class BinarySearchTreeSet<T> : ICollection<T>
{
private readonly IComparer<T> _comparer;
@kpol
kpol / GetAllPermutations.cs
Last active February 13, 2018 09:14
Gets all permutations. Heap's algorithm.
/// <summary>
/// Heap's algorithm.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static IEnumerable<T[]> GetAllPermutations<T>(IEnumerable<T> source)
{
var result = source.ToArray();
@kpol
kpol / Merge.cs
Created March 5, 2018 22:24
Merger two sorted arrays.
/// <summary>
/// Merge two sorted arrays.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static IEnumerable<int> Merge(int[] a, int[] b)
{
int i = 0;
int j = 0;
@kpol
kpol / SwashbuckleFilters.cs
Last active October 3, 2018 03:01
Swashbuckle filters to add enum type into Swagger (OpenAPI) spec file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Description;
using Newtonsoft.Json.Serialization;
using Swashbuckle.Swagger;
namespace Swagger.Configurations
{
public class SwashbuckleDocument
@kpol
kpol / SwaggerXTypeEnumConsolidator.cs
Last active January 30, 2019 22:21
Consolidates enums for Swagger file
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace OpenApi.Extensions
{
/// <summary>
/// Consolidates enums based on <code>x-type-fullname</code> vendor extension.
/// <example>
/// {