Skip to content

Instantly share code, notes, and snippets.

public static class WinForms
{
public static Form FindMainForm()
{
return Form.FromHandle(Process.GetCurrentProcess().MainWindowHandle) as Form;
}
}
@neremin
neremin / SPFieldMultiChoiceExtensions.cs
Last active August 29, 2015 14:07
SharePoint's MultiChoice field extensions for Mappings. Tested on SP2013 Foundation.
/// <devdoc>
/// Based on Andrey Markeev's example http://sharepoint.stackexchange.com/a/28522
/// </devdoc>
public static class SPFieldMultiChoiceExtensions
{
public const string MultiValueSeparator = ";#";
private const string ValueAttributeName = "Value";
private const string MappingsElementName = "MAPPINGS";
private const string MappingElementName = "MAPPING";
@neremin
neremin / SPDataFormCustomSaveLogic.aspx.cs
Last active August 29, 2015 14:08
Intercepting SharePoint's SaveButton action to decorate/replace saving logic. Tested on SP 2013 Foundation
// On your page with SaveButton you could do the following trick
// (in my case save button is added in DataFormWebPart's XSL markup):
SPContext itemContext;
DataFormWebPart dataForm; // from designer's code behind
void Page_Init(object sender, EventArgs e)
{
// NOTE: by some reason ItemContexts of controls in DFWP are differ,
// so only SaveButton's OnSaveHandler is invoked
@neremin
neremin / LinqExtensions.cs
Last active April 10, 2018 14:16
LINQ extensions
public static class LinqExtensions
{
/// <summary>
/// Final method, instructing to iterate through sequence elements immediately.
/// </summary>
/// <typeparam name="T">Type of sequence element.</typeparam>
/// <param name="source">Processed sequence.</param>
public static void Do<T>(this IEnumerable<T> source)
{
using (var e = source.GetEnumerator())
@neremin
neremin / Unidecode.cs
Last active March 10, 2024 05:29
US-ASCII transliterations of Unicode text. It supports almost all unicode letters, including Chinese, Cyrillic, Umlauts and etc.
using System;
using System.Text;
/*
COPYRIGHT
Character transliteration tables:
Copyright 2001, Sean M. Burke <sburke@cpan.org>, all rights reserved.
Python code:
Copyright 2009, Tomaz Solc <tomaz@zemanta.com>
public static class StringExtensions
{
static readonly WeakLazy<char[]> InvalidFileNameChars = new WeakLazy<char[]>(Path.GetInvalidFileNameChars);
static readonly WeakLazy<char[]> InvalidPathChars = new WeakLazy<char[]>(Path.GetInvalidPathChars);
[Pure]
public static bool IsValidFileName(this string name)
{
return !string.IsNullOrEmpty(name) && name.IndexOfAny(InvalidFileNameChars.Value) == -1;
public static class BytesHelper
{
public static bool Equals(byte[] arrayA, byte[] arrayB)
{
Contract.Requires<ArgumentNullException>(arrayA != null);
Contract.Requires<ArgumentNullException>(arrayB != null);
var length = arrayA.Length;
return length == arrayB.Length && UnsafeEquals(arrayA, 0, arrayB, 0, length);
}
public abstract class WeakLazy<T> where T : class
{
protected readonly Func<T> valueFactory;
internal protected WeakLazy(Func<T> valueFactory)
{
Contract.Requires<ArgumentNullException>(valueFactory != null);
this.valueFactory = valueFactory;
}
public abstract T Value { get; }
public static class InterlockedFactory
{
public static T GetOrCreate<T>(ref T value, Func<T> valueFactory) where T : class
{
if (ReferenceEquals(value, null))
{
if (valueFactory == null)
{
throw new ArgumentNullException("valueFactory");
}
@neremin
neremin / InvokeScript.tasks
Created May 12, 2015 16:23
Provide means for invoking inline PS scripts from MSBuild
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- USAGE EXAMPLE
<PropertyGroup>
<MyScript><![CDATA[
function Foo()
{
$targetFileName = args[0]
$targetPdbName = args[1]
$targetPath = args[2]