3 ms·
A closure is also a poor man's class (sort of). I've often wished that C# let me implement interfaces with anonymous classes. Since it doesn't, I get around it
by noblethrasher 16y ago
A closure is also a poor man's class (sort of).
I've often wished that C# let me implement interfaces with anonymous classes. Since it doesn't, I get around it by creating AdHoc classes for interfaces that I use a lot.
For instance, if I have an interface Foo defined as:
interface Foo
{
string Bar(int x);
}
then I also create a corresponding class called AdHocFoo defined like so:
class AdHocFoo : Foo
{
Func<int, string> _bar;
public AdHocFoo(Func<int, string> bar)
{
_bar = bar;
}
public string Bar(int x)
{
return _bar(x);
}
}
I've written a little program that creates these AdHoc class definitions from interfaces.