1
|
// Experiment with extension methods and C5, 2007-10-31
|
2
|
|
3
|
// Compile with
|
4
|
// csc /r:C5.dll Extension.cs
|
5
|
|
6
|
using System;
|
7
|
using System.Linq.Expressions;
|
8
|
using C5;
|
9
|
using SCG = System.Collections.Generic;
|
10
|
|
11
|
namespace Extension {
|
12
|
static class AddOn {
|
13
|
public static int Added<T>(this ICollection<T> coll, int x) {
|
14
|
return coll.Count + x;
|
15
|
}
|
16
|
|
17
|
public static SCG.IEnumerable<T> Where<T>(this ICollection<T> coll,
|
18
|
Expression<Func<T,bool>> pred)
|
19
|
{
|
20
|
Console.WriteLine("hallo");
|
21
|
// Func<T,bool> p = pred.Compile();
|
22
|
Delegate p = pred.Compile();
|
23
|
foreach (T item in coll)
|
24
|
// if (p(item))
|
25
|
if ((bool)p.DynamicInvoke(item))
|
26
|
yield return item;
|
27
|
}
|
28
|
|
29
|
static void Main(String[] args) {
|
30
|
HashSet<Person> hs = new HashSet<Person>();
|
31
|
hs.Add(new Person("Ole"));
|
32
|
hs.Add(new Person("Hans"));
|
33
|
foreach (Person q in (from p in hs where p.name.Length == 4 select p))
|
34
|
Console.WriteLine(q);
|
35
|
}
|
36
|
}
|
37
|
|
38
|
class Person {
|
39
|
public readonly String name;
|
40
|
|
41
|
public Person(String name) {
|
42
|
this.name = name;
|
43
|
}
|
44
|
|
45
|
public override String ToString() {
|
46
|
return name;
|
47
|
}
|
48
|
}
|
49
|
}
|