问题
Let's say we have a class called MyClass.
public class MyClass
We also have an interface like so:
public interface MyInterface{
public string SomeFunction(int foo, string bar, short baz){}
}
We want this class to inherit from MyInterface.
public class MyClass: MyInterface
MyInterface has n properties, and i methods. How can I get Visual Studio to automatically implement all those methods and properties without the developer doing any of the legwork?
回答1:
For C#, you can right click the 'MyInterface' and choose 'Implement Interface' from the context menu. I believe there is a similar (but slightly different) shortcut for VB.NET.
回答2:
You can simply use shortcut "CTR+." to do that. Position your cursor on MyInterface of "public class MyClass: MyInterface" line, and press "CTR+.", a context menu will show up with "Implement Interface ...".
回答3:
To automatically implement all inherited methods from an interface:
C# : Just right click on interface(Prefix with 'I') name and select implement interface option.
VB.NET : Just point your cursor after interface(Prefix with 'I') name and hit enter.
Enjoy!!!
回答4:
This is a job for macros in Visual Studio. Here is an example that does a similar thing for abstract classes. It should be enough to get you going.
回答5:
In Visual Studio 2013 you can right click on the interface and select "Implement Interface". It will generate all the methods for you with the NotImplmentedException.
来源:https://stackoverflow.com/questions/884075/visual-studio-automatically-implement-all-inherited-methods-from-an-interface