工具来生成代表团接口实现? [英] Tool to generate interface implementation by delegation?
问题描述
我经常需要通过委派执行到我的类的成员,以实现一个接口。这个任务是相当繁琐,因为即使,Visual Studio生成存根接口方法,我还是要编写代码来实现委托。它不需要太多的思考,因此它可能可能是一个代码生成工具可以自动...
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 ?
编辑:看来, ReSharper的可以做到这一点,但它是相当昂贵...有具有相同功能的免费替代品?
EDIT : it seems that ReSharper can do it, but it's pretty expensive... is there a free alternative with the same feature ?
推荐答案
我一直在使用ReSharper的几个月,现在,它有一个很大的特点,以做到这一点。
I've been using Resharper for a few months now, and it has a great feature to do this.
例如,写下面的代码:
class MyList<T> : IList<T>
{
private readonly IList<T> _list;
}
放在 _list $ C $插入符号C>,按<大骨节病>替代骨节病> + <大骨节病>插件骨节病>(快捷键生成代码),然后选择委派成员。选择您需要的成员,和R#产生委派成员为他们:
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; }
}
这篇关于工具来生成代表团接口实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!