Question

I often need to implement an interface by delegating the implementation to a member of my class. This task is quite tedious because, even though Visual Studio generates stubs for the interface methods, I still have to write the code to delegate the implementation. It doesn't require much thinking, so it could probably be automated by a code generation tool...

I'm probably not the first one to think of this, so there must be such a tool already, but I couldn't find anything on Google... Any idea ?


EDIT : it seems that ReSharper can do it, but it's pretty expensive... is there a free alternative with the same feature ?

Was it helpful?

Solution

I've been using Resharper for a few months now, and it has a great feature to do this.

For instance, write the following code:

class MyList<T> : IList<T>
{
    private readonly IList<T> _list;
}

Place the caret on _list, press Alt + Ins (shortcut for Generate Code), and select "Delegating members". Select the members you need, and R# generates delegating members for them:

    public void Add(T item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(T item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return _list.IsReadOnly; }
    }

    public int IndexOf(T item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public T this[int index]
    {
        get { return _list[index]; }
        set { _list[index] = value; }
    }

OTHER TIPS

... see dynamic proxy...

However...

The ability to delegate interface implementation to an instance variable or type [as in typeof(type)], is a greatly needed language feature of C# and VB.Net. With the absence of multiple inheritance and the prevalence of sealed and non-virtual methods and properties, the amount of fluff code required is daunting.

I would think the following language enhancement would work... //C#

interface IY_1
{
  int Y1;
  int Y2;
}
...
..
interface IY_n
{
....
..
}


class Y : IY_1, IY_2, ....,IY_n
{
  private readonly Oy_1 Oy_1 = new Oy_1() supports IY_1, IY_2,...etc;   // <<-----
  private readonly Oy_2 Oy_2 = new Oy_2() supports IY_3, IY_8,...etc;
  public int Y2 {...}
}

The "supports" keyword (or equivalent such as "defaults") would identify an ordered list of class field values that "assist" in the implementation of one or more interfaces, using the normal name and signature mappings semantics of interface of name and signature correspondence. Any local implementation would have precedence and, as well, a single interface may be implemented by multiple field values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top