2 ms·
Not sure if I interpreted the problem correctly, but in C# composing interfaces and using generic extension methods with type predicates: interface IFooAble {
by zwerdlds 10y ago
Not sure if I interpreted the problem correctly, but in C# composing interfaces and using generic extension methods with type predicates:
interface IFooAble { ... }
interface IBarAble : IFooAble { ... }
static class FooProcessor {
static T ProcessFoo<T>(T fooInst)
where T: IFooAble { ... }
}
This gives us something that as long as the object implements IFooAble, we can use this extension for free. I.E: IBarAble can use ProcessFoo just fine. This works really well for writing fluent interfaces as demoed above.
- virmundi 10y agoThe question is can you do this to sealed/final classes like String? Say you wanted a Canadian String that appended ",eh" ever time it saw a question mark, could you do this? Could you make Money do the same?
- zwerdlds 10y agoYeah, AFAIK, extension methods just have to work on POCOs, which string and money (or decimal) are.
- deleted 10y ago[deleted]