locked
Extensibility pointers for creating code after properties have been "decorated" with .net attributes RRS feed

  • Question

  • Hi, could somebody point me on the right direction? I am new to VSX.

    I'd like to find the properties decorated with my .NET attributes and extend this class (with a Partial class) when it is being saved or when it is being edited (preferably)

    Thanks for any assistance that you may provide.


    Julio Santos
    Sunday, September 18, 2011 4:25 PM

Answers

  • Hi Julio,

    It's more problematic that you might think. Reflection would be my first suggestion, but as you mentioned it requires some sort of background build, and if you load the assembly into the IDE to query the various types, you'll lock the assembly and won't be able to rebuild it without restarting the IDE. :-(

    I don't think the codemodel api's are granular enough to allow you to pick various attributes off a class definition, so you'll likely have to revert to parsing the sources yourself. Also, I don't think you can use an SFG to 'extend' a class or object. An SFG be definition takes a file and generates a single file for it. If you were to assign an SFG to your code file, I would expect it to no longer be included in the build. Rather the output of the SFG would be included in the build.

    Sincerely,


    Ed Dore
    • Marked as answer by Yi Feng Li Friday, October 7, 2011 1:14 AM
    Thursday, September 22, 2011 3:21 AM
  • Hi

     

    >>Do you have any recommendations for creating (or editing) a file inside the VS project?

     

    You can use Visual Studio Automation object (DTE) to add files into the project and/or edit the code via codemodel.  

     

    The detail information about access the code model, please check Discovering Code with the Code Model, http://msdn.microsoft.com/en-us/library/ms228763.aspx.  

     

    Controlling Projects and Solutions and Creating and Controlling Environment Windows are also related to your topic, which illustrates how to control projects and solutions by using Visual Studio automation objects and explains how to create and control tool windows, change window characteristics, automate text search and replace, control macros, control tools options settings, discover code with the Code model, and manipulate tree views by using UIHierarchy.

     

    The detail DTE structure, please see http://msdn.microsoft.com/en-us/library/za2b25t3.aspx.

     

    Hope the information helps,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Yi Feng Li Friday, October 7, 2011 1:14 AM
    Thursday, September 29, 2011 2:37 AM

All replies

  • Not quite understand "find the properties decorated with my .NET attributes ". Could you explain your requirement a little bit more?

    To get started with VS SDK, you can read http://msdn.microsoft.com/en-us/library/bb166441.asp

    Tuesday, September 20, 2011 8:17 AM
  • sure. as documented at http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx

    Declaring an Attribute Class

    Declaring an attribute in C# is simple — it takes the form of a class declaration that inherits from System.Attribute and has been marked with the AttributeUsage attribute as shown below:  ...

    those attributes are usually determined by using Reflection, but this requires that the project be Built first, which is not pratical.

    I'd like to parse the C# class being edited (maybe by capturing Editor events, by using a SingleFileGenerator custom tool or a combination of both) to extend the original class.

    For instance, a property decorated with a .NET attribute

    partial MyClass {
    [MyStaticProperty]

    public string MyProperty { get; set; }
    }

    could create or update another file with the following static property...

    partial MyClass {
    public static string MyStaticProperty { get; set; }
    }

    Please, let me know if you need further clarifications.

    Thanks,


    Julio Santos
    Wednesday, September 21, 2011 2:54 AM
  • Hi Julio,

    It's more problematic that you might think. Reflection would be my first suggestion, but as you mentioned it requires some sort of background build, and if you load the assembly into the IDE to query the various types, you'll lock the assembly and won't be able to rebuild it without restarting the IDE. :-(

    I don't think the codemodel api's are granular enough to allow you to pick various attributes off a class definition, so you'll likely have to revert to parsing the sources yourself. Also, I don't think you can use an SFG to 'extend' a class or object. An SFG be definition takes a file and generates a single file for it. If you were to assign an SFG to your code file, I would expect it to no longer be included in the build. Rather the output of the SFG would be included in the build.

    Sincerely,


    Ed Dore
    • Marked as answer by Yi Feng Li Friday, October 7, 2011 1:14 AM
    Thursday, September 22, 2011 3:21 AM
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 26, 2011 3:39 AM
  • Hi Ed, thanks for clarifying. I see your point. I guess the only alternative left is to capture editing events. Do you know of any good article on that? Also, I still will have to write a separated file to extend the class (partial class). Ideally, this file should be dependent of the original one. Do you have any recommendations for creating (or editing) a file inside the VS project? Thanks for your assistance,
    Julio Santos
    Monday, September 26, 2011 1:00 PM
  • Hi Yi, I still have some questions left. Thanks for following up.
    Julio Santos
    Monday, September 26, 2011 1:02 PM
  • Hi

     

    >>Do you have any recommendations for creating (or editing) a file inside the VS project?

     

    You can use Visual Studio Automation object (DTE) to add files into the project and/or edit the code via codemodel.  

     

    The detail information about access the code model, please check Discovering Code with the Code Model, http://msdn.microsoft.com/en-us/library/ms228763.aspx.  

     

    Controlling Projects and Solutions and Creating and Controlling Environment Windows are also related to your topic, which illustrates how to control projects and solutions by using Visual Studio automation objects and explains how to create and control tool windows, change window characteristics, automate text search and replace, control macros, control tools options settings, discover code with the Code model, and manipulate tree views by using UIHierarchy.

     

    The detail DTE structure, please see http://msdn.microsoft.com/en-us/library/za2b25t3.aspx.

     

    Hope the information helps,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Yi Feng Li Friday, October 7, 2011 1:14 AM
    Thursday, September 29, 2011 2:37 AM
  • Hello,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 2:11 AM