通过委托生成接口实现的工具?
我经常需要通过将实现委托给类的成员来实现接口。 这项任务非常繁琐,因为即使 Visual Studio 为接口方法生成存根,我仍然必须编写代码来委托实现。 它不需要太多思考,所以它可能可以通过代码生成工具自动化...
我可能不是第一个想到这一点的人,所以肯定已经有这样的工具了,但我找不到谷歌上有什么...有什么想法吗?
编辑:似乎 ReSharper 可以做到这一点,但它相当昂贵...是否有具有相同功能的免费替代品?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试以下操作: http://www.codeproject.com/KB/codegen/decorators.aspx?display=PrintAll&fid=1532149&df=90&mpp=25& ;noise=3&sort=Position&view=Quick&select=2837870
You could try this : http://www.codeproject.com/KB/codegen/decorators.aspx?display=PrintAll&fid=1532149&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2837870
...请参阅动态代理...
但是...
将接口实现委托给实例变量或类型[如 typeof(type)] 的能力是 C# 和 VB.Net 非常需要的语言功能。 由于缺乏多重继承以及密封和非虚拟方法和属性的盛行,所需的无用代码量令人望而生畏。
我认为以下语言增强会起作用......
//C#
“supports”关键字(或等价物,例如“defaults”)将使用接口的正常名称和签名映射语义来标识类字段值的有序列表,这些字段值“协助”实现一个或多个接口姓名和签名对应。 任何本地实现都具有优先权,并且单个接口也可以由多个字段值实现。
... 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#
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.
我已经使用 Resharper 几个月了,它有一个很棒的功能可以做到这一点。
例如,编写以下代码:
将脱字符号放在
_list
上,按 Alt + Ins(生成代码的快捷方式),然后选择“委派”成员”。 选择您需要的成员,R# 会为他们生成委派成员: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:
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: