/
generic-method-patching.2.cs
65 lines (49 loc) · 1.21 KB
/
generic-method-patching.2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
public class MyDict<S,T> {
public void Add (S key, T value) {
S[] sa = new S[1];
T[] ta = new T[1];
sa[0] = key;
ta[0] = value;
}
}
public abstract class FastFunc<S,T> {
public abstract S Invoke (T bla);
}
public class StringFastFunc : FastFunc<string, int> {
public override string Invoke (int bla) {
return bla.ToString ();
}
}
public class ArrayFastFunc : FastFunc<byte [], int> {
public override byte [] Invoke (int bla) {
return new byte [bla];
}
}
public class IntCache<T> {
MyDict<int,T> cache;
public T Invoke (FastFunc<T,int> f, int bla) {
if (cache == null)
cache = new MyDict <int,T> ();
T value = f.Invoke (bla);
cache.Add (bla, value);
return value;
}
}
public class main {
public static int Main () {
StringFastFunc sff = new StringFastFunc ();
ArrayFastFunc aff = new ArrayFastFunc ();
IntCache<string> ics = new IntCache<string> ();
MyDict<string,string> dss = new MyDict<string,string> ();
dss.Add ("123", "456");
ics.Invoke (sff, 123);
ics.Invoke (sff, 456);
IntCache<byte []> ica = new IntCache<byte []> ();
ica.Invoke (aff, 1);
ica.Invoke (aff, 2);
ica.Invoke (aff, 3);
return 0;
}
}