views:

155

answers:

1

Eclipse allows us to define a class as:

interface MyInterface {
    void methodA();
    int methodB();
}

class A : MyInterface {
    MyInterface myInterface;
}

and then with this "Generate delegate methods", it will implement all needed methods for the interface, redirecting their logic to myInterface's methods:

class A : MyInterface {
    MyInterface myInterface;

    public void methodA() {
        myInterface.methodA();
    }

    public int methodB() {
        return myInterface.methodB();
    }
}

Is it possible to accomplish the same with VS2010? And with R#?

Thanks

+3  A: 

With Resharper you can do this.

http://www.jetbrains.com/resharper/features/code_generation.html

Generate Delegating Members

Delegating members provides a means to encapsulate some behavior or publish methods of a class's field through the class's own interface. Specify fields and their methods, and ReSharper will generate wrapping methods in the current class.

This is a great feature that we use all the time. There are a few ways to access it but what I do is hit ALT-INS which brings up the Generate context menu. A few items down on the list is Delegating members. Then you get a tree from which you select the fields (objects) you want to delegate to and which delegating properties/methods you want to create. Very quick and easy and works great.

Sam
Yes, but it doesn't explain how to accomplish it. Have you ever done it, by chance?
devoured elysium
Ah, found it under Code Generation. This was obvious :(. Ty
devoured elysium
@devoured elysium, yeah, I use this feature all the time. I'll edit my answer to add more info.
Sam