Skip to content

Instantly share code, notes, and snippets.

@mstrobel
Last active August 29, 2015 14:25
Show Gist options
  • Save mstrobel/7df0918b2072451d587c to your computer and use it in GitHub Desktop.
Save mstrobel/7df0918b2072451d587c to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class C {
public static void M(object[] items) {
var cancelableItems = items.Cast<dynamic>()
.Where(d => d.CanCancel())
.ToArray();
//
// o.OrderId dynamically bound without problem with csc.exe in VS2013.
//
var tasks1 = cancelableItems.ToArray(o => F((long)o.OrderId));
//
// Works fine if lambda parameter is explicitly typed.
//
var tasks2 = cancelableItems.ToArray((dynamic o) => F((long)o.OrderId));
}
private static long F(long id) {
return id;
}
public static TResult[] ToArray<TResult>(this IEnumerable items, Func<object, TResult> selector)
{
return ToArray(items.Cast<object>(), selector);
}
public static TResult[] ToArray<TSource, TResult>(this IEnumerable<TSource> items, Func<TSource, TResult> selector)
{
return items.Select(selector).ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment