Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool that will implement an interface by wrapping a member field or property?

I find myself doing the following often enough that I feel like there must be an automated solution:

I have a wrapper class, say ListWrapper, which wraps an IList:

public class ListWrapper : IList 
{
    private IList _list;

    // ... Implement IList by redirecting every call to _list
}

Is there any tool out there that will automatically generate this implementation?

like image 279
afeygin Avatar asked Apr 10 '12 19:04

afeygin


People also ask

How to wrap an interface in a class?

add the interface to the class you wish to be the wrapping class class MyWebElement : IWebElement { } Find/Click "Delegate implementation of "YourInterfaceHere" to a new field Select your options Click finish and enjoy your new class

What is the use of interface in a class?

A class that implements interface must implements all the methods in interface. All the methods are public and abstract. And all the fields are public, static, and final. It is used to achieve multiple inheritance. It is used to achieve loose coupling.

How to implement interface in Java?

That means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.

How to wrap properties of an interface in ReSharper?

class MyWrapper : IExternalClass 4 - You will get a hint on the class name about members from the interface not being implemented. Alt + Enter on it and let Resharper automatically implement them 5 - Use this code template to wrap properties


2 Answers

Using ReSharper, inside the class hit "alt-insert" and then select "delegating members".

like image 107
David Peden Avatar answered Oct 20 '22 00:10

David Peden


Any mocking framework, and most of those use Castle Dynamic Proxy IIRC.

Take a look at Moq

Actually, here's a similar question's answer:

auto create derived types

like image 38
Ian Avatar answered Oct 19 '22 22:10

Ian