Skip to content

Instantly share code, notes, and snippets.

View dgrunwald's full-sized avatar

Daniel Grunwald dgrunwald

View GitHub Profile
class Program
{
public static void Main(string[] args)
{
bool b = false;
short s = 0;
var res = b ? 1L : s;
Console.WriteLine(res.GetType()); // prints Int64
}
}
@dgrunwald
dgrunwald / DecompileQueries.cs
Created August 21, 2012 10:58
Query Expression Decompiler
// Uses ICSharpCode.NRefactory 5.2
using System;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.PatternMatching;
namespace DecompileQueries
{
class Program
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
[CompilerGenerated]
[StructLayout(LayoutKind.Auto)]
private struct <CopyToAsyncInternal>d__2 : IAsyncStateMachine
{
@dgrunwald
dgrunwald / gist:2391396
Created April 15, 2012 09:12
async decompiler
// Original:
public async Task<bool> SimpleBoolTaskMethod()
{
Console.WriteLine("Before");
await Task.Delay(TimeSpan.FromSeconds(1.0));
Console.WriteLine("After");
return true;
}
// MoveNext
@dgrunwald
dgrunwald / gist:2158660
Created March 22, 2012 14:31
Adorner in ScrollViewer
void AddLayer(ScrollViewer scrollViewer)
{
scrollViewer.ApplyTemplate();
ScrollBar scrollBar = (ScrollBar)scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer);
scrollBar.ApplyTemplate();
Track track = (Track)scrollBar.Template.FindName("PART_Track", scrollBar);
var grid = VisualTreeHelper.GetParent(track) as Grid;
if (grid != null) {
var myElement = new Border();
Grid.SetColumn(myElement, Grid.GetColumn(track));
@dgrunwald
dgrunwald / async.cs
Created March 2, 2012 20:33
Async/Await support for .NET 4.0
// Copyright (c) 2012 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
@dgrunwald
dgrunwald / awaitableWPF.cs
Created March 2, 2012 20:30
Awaitable WPF Dispatcher
public static class WpfExtensions
{
public static DispatcherAwaiter GetAwaiter(this Dispatcher dispatcher)
{
return new DispatcherAwaiter(dispatcher, DispatcherPriority.Normal);
}
public static Tuple<Dispatcher, DispatcherPriority> WithPriority(this Dispatcher dispatcher, DispatcherPriority priority)
{
return Tuple.Create(dispatcher, priority);
public IType Resolve(ITypeResolveContext context)
{
string[] parts = typeName.Split('.');
var assemblies = new [] { context.CurrentAssembly, context.Compilation.MainAssembly }.Concat(context.Compilation.ReferencedAssemblies);
for (int i = parts.Length - 1; i >= 0; i++) {
string ns = string.Join(".", parts, 0, i);
string name = parts[i];
int topLevelTPC = (i == parts.Length - 1 ? typeParameterCount : 0);
foreach (var asm in assemblies) {
if (asm == null)
@dgrunwald
dgrunwald / gist:1022708
Created June 13, 2011 12:44
NR memory usage test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace TestNRMemoryUsage
{
@dgrunwald
dgrunwald / gist:835364
Created February 19, 2011 21:06
Pattern Matching Idea
var usingVarDecl = new VariableDeclarationStatement {
Type = Pattern.Any("type"),
Variables = {
Pattern.NamedGroup(
"variable",
new VariableInitializer {
Initializer = Pattern.Any()
}
)
}