开发者

How to generate a class from an interface

开发者 https://www.devze.com 2023-01-12 03:01 出处:网络
Is there a way in Visual Studio to right click an interface and \'Generate Class from Inter开发者_如何学Goface\' so I end up with an empty class with all the properties and methods which the interface

Is there a way in Visual Studio to right click an interface and 'Generate Class from Inter开发者_如何学Goface' so I end up with an empty class with all the properties and methods which the interface requires?

Kind of like Extract Interface, but backwards.


Not quite what you're asking for, but if you create a class, and declare it as implementing your interface, you can right click on the interface and select "Implement Interface." This will add the appropriate methods to your class.


I believe the answer before was for C#, but if you are using VB.NET all you need to do is type in the Implements IMyInterface the line below the class declaration and press <Enter>. It automatically generates all of the method and property signatures for you. You can also always go back to the Implements line and press again if any new methods or properties were added to the Interface and they too will be generated for you.


To specifically address this question, yes, there is. Caveat, I'm using C#, working with the Visual Studio 2013 Ultimate Edition.

Create an empty class, like "Class1":

class Class1
{
}

Then append an interface name, such as IObjectBase (which I'll continue to use for the remainder of this post), by using the ":", as in the following example:

class Class1:IObjectBase 

When you do, there will be a short, thick, white underline, under the first letter of your interface name "I". Mouse-over it. There are a number of options which may appear. You're looking for a small icon which looks like a page with a dog-eared, top-right corner as you hover over the small, thick white line, with the tooltip of "Options to implement interface". Click on the drop-down arrow to its right.

You'll be presented with two menu item options: "Implement interface 'IObjectBase'" and "Explicitly implement interface 'IObjectBase'". From there the choice is yours.

If you're looking for guidance regarding the two options, I'd suggest reading the following articles, I think each covers the topic well, yet from different viewpoints:

  • http://www.codeproject.com/Articles/392516/Why-I-use-explicit-interface-implementation-as-a-d
  • https://softwareengineering.stackexchange.com/questions/136319/whats-the-difference-between-implementing-an-interface-explicitly-or-implicitly

Regards,

HALAR

0

精彩评论

暂无评论...
验证码 换一张
取 消