locked
convert implicit declaration to explicit declaration RRS feed

  • Question

  • VS2010 / C#

     

    I'm trying to write a macro/add-in that will convert implicit C# declarations (var) to explicit declarations.

    I am able to scan the source-file and find each 'var' declaration but have found no procedure to follow to find the corresponding explicit type. I know the information is available in the IDE because when the cursor hovers over a 'var' declaration the explicit type is displayed in a bubble.

    I currently examine the right-hand side (RHS) of the declaration and in some cases (e.g. 'new' expression, cast, 'as' expression) I can determine the explicit type. In other cases my macro makes an educated guess; and in some cases it just gives up.

    One method I thought about was to examine the 'var' declaration's Method's MethodInfo. But information about local variables is stored by ordinal only and I don't want to scan the Method for all declarations.

    I am hoping that this functionality is publicly available to macro/add-in writers but that I have not been able to find it.

    Thanks in advance.

    --
    Lee Silver

    Monday, January 3, 2011 12:41 AM

Answers

  • He Lee,

    Code information inside methods is not available to VS extensions, only the parameters of the method. So you have to parse yourself and try to guess the var types.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by Victor_Chen Monday, January 10, 2011 8:01 AM
    Monday, January 3, 2011 9:32 AM

All replies

  • He Lee,

    Code information inside methods is not available to VS extensions, only the parameters of the method. So you have to parse yourself and try to guess the var types.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by Victor_Chen Monday, January 10, 2011 8:01 AM
    Monday, January 3, 2011 9:32 AM
  • Hi Lee,

     

    I am agree with Carlos, if you have further questions, be free to post another thread.

     

    Best Regards,

    Ziwei Chen

     

     

     


    Ziwei Chen [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, January 10, 2011 8:01 AM