Creating a custom properties editor/generator in Visual Studio .Net
I have an application that lets developers create their own derived subclasses and add predetermined properties to it.
For example :
class MyD开发者_Python百科ocContainer : GenericDocumentContainer {
Document docA = new Document("Secret Report on Brocoli Wars");
Document docB = new Document("Who's afraid of Donald Duck");
Document docC = new Document("Out Of Money Experience");
//rest of code goes here
}
I would like to have a visual editor that would allow to edit/add documents and other predefined types to a partial class (just like the settings designer does)
This would really help having a synthetic view of the derived classes and make it easier to maintain.
Ideally, I would also like to generate few related code lines for each property in special places (like constructor or dedicated init method)
Any Ideas ?
From what you are trying to achieve (and asuming you want this to be as natural as possible), I would suggest you to use Visual Studio Add-In or VSX Package, accessing the DTE and its code model.
Here is what you may want to do (in case you choose the Add-In path):
- Create Add-In (from the Other Project Types menu there is an AddIn option).
- Create Windows Form or Windows Use Control or WPF Control to accomodate the UI that will be used.
- Show the control or form so the developer can do his / her work.
- Using the FileCode model approach add the code artifacts to the solution.
Some of the things you need to know:
- You will need DTE / DTE2 instances, the AddIn already have them, but you may need to pass them to the UI in case you will be changing the code from there.
- The DTE has Solution property, which represents the solution that is currently loaded. You may want to iterate its Projects / Items.
- Each Project has project items (the actual code files).
- Some of the ProjectItems have FileCodeModel.
Having FileCodeModel for the item you want to add code artifacts to, it should be no problem to add CodeClasses, CodeFunctins and other things pretty easy.
PLEASE NOTE: Extending Visual Studio may be tricky (due to the COM nature of the extensibillity framework) so please be sure to create a good Proof Of Concept first.
精彩评论