Skip to content

Instantly share code, notes, and snippets.

View SchreinerK's full-sized avatar

Kay-Uwe Schreiner (kux) SchreinerK

View GitHub Profile
[SuppressMessage("ReSharper", "VirtualMemberNeverOverridden.Global", Justification = "Public API")]
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Public API")]
public class NotifyPropertyChangedBase : INotifyPropertyChanged {
/// <summary>
/// Sets a backing field value and if it's changed raise a notification.
/// </summary>
/// <typeparam name="T">The type of the value being set.</typeparam>
/// <param name="oldValue">A reference to the field to update.</param>
/// <param name="newValue">The new value.</param>
@SchreinerK
SchreinerK / DesignDataContext.xaml
Created October 1, 2019 08:51
Design DataContext
<d:DesignProperties.DataContext>
<$ViewModel$ />
</d:DesignProperties.DataContext>
d:DataContext="{d:DesignInstance Type=$ViewModel$, IsDesignTimeCreatable=False}"
@SchreinerK
SchreinerK / RegisterPackUriScheme.cs
Last active September 20, 2019 13:20
Problem with pack URIs in unit tests.
// http://jake.ginnivan.net/pack-uri-in-unit-tests/
PackUriHelper.Create(new Uri("reliable://0")); // register pack-uri
@SchreinerK
SchreinerK / MockServices.cs
Last active September 28, 2019 13:59
TestTools MockServices
// Source: https://gist.github.com/SchreinerK/73002d709fe6ee365cd3d7b43186300c#file-mockservices-cs
// Version: 1.6
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
// ReSharper disable LocalizableElement
// ReSharper disable CheckNamespace
@SchreinerK
SchreinerK / DependencyPropertyValueChangedEvent.cs
Created September 6, 2019 13:33
DependencyProperty ValueChanged event
var dpd = DependencyPropertyDescriptor.FromProperty(TextEditor.IsModifiedProperty, typeof(TextEditor));
dpd.AddValueChanged(_textEditor, TextEditor_IsModifiedChanged);
dpd.RemoveValueChanged(_textEditor, TextEditor_IsModifiedChanged);
@SchreinerK
SchreinerK / GenerateFullTypeName.cs
Created September 1, 2019 06:06
Generates the (C#) code for the specified type name.
public static string GenerateFullTypeName(Type type)
{
var retType = new StringBuilder();
if (type.IsGenericType)
{
var parentType = type.FullName.Split('`');
var arguments = type.GetGenericArguments();
var argList = new StringBuilder();
@SchreinerK
SchreinerK / AsyncHelpers.cs
Created July 21, 2019 03:59
Run Task synchonously
public static class AsyncHelpers
{
/// <summary>
/// Execute's an async Task<T> method which has a void return value synchronously
/// </summary>
/// <param name="task">Task<T> method to execute</param>
public static void RunSync(Func<Task> task)
{
var oldContext = SynchronizationContext.Current;
var synch = new ExclusiveSynchronizationContext();
@SchreinerK
SchreinerK / WaitHandleAsyncExtension.cs
Created July 10, 2019 05:37
WaitHandle.WaitOneAsync(...)
public static class WaitHandleAsyncExtension
{
public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle registeredHandle = null;
var tokenRegistration = new CancellationTokenRegistration();
try
{
var tcs = new TaskCompletionSource<bool>();
registeredHandle = ThreadPool.RegisterWaitForSingleObject(
@SchreinerK
SchreinerK / Result.cs
Created July 5, 2019 04:43
Result<Value>
public struct Result<TValue>
{
internal Result(TValue value, bool success, object failureInfo)
{
Value = value;
Success = success;
FailureInfo = failureInfo;
}
public TValue Value { get; private set; }
@SchreinerK
SchreinerK / GuidSemaphoreSlim.cs
Last active July 3, 2019 08:08
Guid Ressource Lock
public sealed class GuidSemaphoreSlim : IDisposable
{
private static readonly Dictionary<Guid,SemaphoreSlim> Handles=new Dictionary<Guid, SemaphoreSlim>();
private readonly Guid _guid;
private readonly SemaphoreSlim _semaphore;
/// <summary>
/// Usage: using(await GuidSemaphoreSlim.WaitAsync(myGuid)) {...}
/// </summary>
public static async Task<IDisposable> WaitAsync(Guid guid)