Visual Studio 2010: Easiest way to duplicate a class?

非 Y 不嫁゛ 提交于 2020-01-11 09:23:20

问题


Is there an easy way to duplicate a class with a different name?


回答1:


Not sure whether this can be qualified as the easiest way but if you have ReSharper, you can use its Copy Type refactoring to copy classes/interfaces/structs with control over the namespace the copy is landing in and naming within the copy - which means that if you're copying a class with 5 constructors, the copy will have all of them renamed to match the name of the new class.

However, depending on what you're trying to achieve, using Extract Interface or Extract Superclass might be a better option.




回答2:


  • Open file in Visual Studio.
  • Press Ctrl+A.
  • Press Ctrl+C.
  • Create a new file for the new class.
  • Press Ctrl+V in the new file.
  • Replace all old class names with the new one. (Ctrl+H)



回答3:


No, there's no refactoring tool for duplicating a class but with different name.

I would imagine that the reason why this feature is not present is because duplicating code is generally considered a bad idea. I'd suggest instead changing your class into a base class and then make two derived classes from it, overriding methods where you need to change the behaviour.




回答4:


Refactor to give a new name, copy, undo, paste :-) Remember the undo! This will rename the constructors and finalizers!




回答5:


Copy and paste and give the class a new name ?

It is generally not a good idea just to copy code and give it a new class name. It appears you may want something to do with inheritance / interfaces?

How about creating a base class and then use that base class for further classes that have the same / close to the same functionality.




回答6:


It's usually better to refactor your class to be reusable. Copy paste code leads to having to fix the same code in multiple places and much pain as you're breaking OO principles.



来源:https://stackoverflow.com/questions/5008893/visual-studio-2010-easiest-way-to-duplicate-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!