I suggest you ...

Add auto implement feature to C# language

This is a pain to wrap a class by an other, and implement inner class intreface on outer class which is exposed by an inner instance.

My suggestion is for example:

interface IFoo
{
void DoSometing();
int GetOtherThing();
}

class Foo : IFoo
{
void DoSometing() { ... }
int GetOtherThing() { ... }
}

interface IStuff
{
void DoNothing();
void PutSomething(int s);
}

class Stuff : IStuff
{
void DoNothing() { ... }
void PutSomething(int s) { ... }
}

class All : IStuff, IFoo
{
public All(Foo foo, Stuff stuff)
{
Foo = foo;
Stuff = stuff;
}

public Foo Foo { get implements IFoo; private set; }

public Stuff Stuff { get implements IStuff; private set; }

}

Or if we have explixit getters:

public Foo Foo
{
get implements IFoo
{
return this.foo;
}
}

Implements keyword can be added after methods as well if they returns a value:

public Foo GetFooFromSomewhere() implements IFoo
{
return this.other.Foo;
}

48 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Gabor MezoGabor Mezo shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    WilWil shared a merged idea: Support Interface Delegation  ·   · 

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • QwertieQwertie commented  ·   ·  Flag as inappropriate

        There are a lot of different cases where people need to wrap an object and forward most of the methods, only changing the behavior of one or two. The majority of situations are known as the "decorator" pattern.

        But there is tremendous variety in the kinds of things one might want to do, so I think the solution that makes the most sense is a compile-time metaprogramming feature that lets you add these kinds of features to C# yourself.

      • Nicole CalinoiuNicole Calinoiu commented  ·   ·  Flag as inappropriate

        While I also think that a mechanism for facilitating interface implementation via composition would be a great idea (https://connect.microsoft.com/visualstudio/feedback/details/526307/add-automatic-generation-of-interface-implementation-via-implementing-member), I have reservations about the proposed mechanism. It feels a bit a "inverted" from the point of any reader of the code, such as a code reviewer or maintainer, who shouldn't have to go digging through the entire class contents to find the implementing member. Might it not be preferable to specify the mapping to the implementing member in the class declaration? e.g.:

        public class All : IStuff(this.Stuff), IFoo(this.Foo)
        {
        //...
        }

        Failing a mapping mechanism at the class level, an attribute would perhaps be a better choice than a keyword for indicating an implementing member since it would allow use of a field as an implementing member, and it would be less likely to be "hidden" in a collapsed region. e.g.:

        public class All : IStuff, IFoo
        {
        [Implements(typeof(IStuff))]
        private readonly IStuff _stuff;

        [Implements(typeof(IFoo))]
        private readonly IFoo _foo;

        //...
        }

        Also, it would be very useful if the mechanism were to support a "manual" override for a subset of the interface implementation. (i.e.: The mechanism shouldn't generate a member for a mapped interface if it already finds an implementation in the class.) For example, if All's implementation of IFoo.DoSomething should do a bit extra, one should be able to simply add an IFoo.DoSomething method in All's code without having to opt out of the rest of the "auto-composition" mechanism.

      Feedback and Knowledge Base