我经常需要通过委派执行到我的类的成员来实现的接口。这个任务是相当繁琐的,因为即使,Visual Studio生成存根接口方法,我仍然需要编写代码来实现委托。它不需要太多的思考,因此它可能可能是一个代码生成工具实现自动化...

我大概不会想到这第一位的,所以必须有已经这样一个工具,但我无法找到谷歌什么...任何想法?


编辑:似乎的ReSharper可以做到这一点,但这是相当昂贵...有具有相同特征的自由选择?

有帮助吗?

解决方案

我一直在使用ReSharper的几个月,现在,它有一个很大的特点来做到这一点。

例如,写入以下代码:

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

放在_list插入符,按替代 + (快捷方式生成代码),并选择 “委派成员”。选择所需的成员,且R#产生委托成员它们:

    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; }
    }

其他提示

...看到动态代理...

...然而

委托接口实现到一个实例变量或类型的能力[按typeof运算(类型)〕,是C#和VB.Net一个非常需要的语言特性。与不存在多重继承的和的密封和非虚拟方法和属性的盛行,绒毛代码所需的时间量是艰巨的。

我想下面的语言增强会工作...     // 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 {...}
}

在“支撑件”的关键字(或等效物如“默认值”)将确定类字段值“辅助”中的一个或多个接口的实施的有序列表,使用正常的名称和签名的映射的接口的语义姓名和签字的对应关系。任何地方执行将具有优先级和,以及,一个单一的接口可以由多个字段值来实现。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top