Is it possible to implement mixins in C#?

前端 未结 9 630
感动是毒
感动是毒 2020-11-29 17:50

I\'ve heard that it\'s possible with extension methods, but I can\'t quite figure it out myself. I\'d like to see a specific example if possible.

Thanks!

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 18:54

    It really depends on what you mean by "mixin" - everyone seems to have a slightly different idea. The kind of mixin I'd like to see (but which isn't available in C#) is making implementation-through-composition simple:

    public class Mixin : ISomeInterface
    {
        private SomeImplementation impl implements ISomeInterface;
    
        public void OneMethod()
        {
            // Specialise just this method
        }
    }
    

    The compiler would implement ISomeInterface just by proxying every member to "impl" unless there was another implementation in the class directly.

    None of this is possible at the moment though :)

提交回复
热议问题