Search code examples
c#c++visual-studio-extensions

EnvDTE.CodeNamespace members are empty?


I am trying to find a function in active document however I am struggling with a problem, when the function is inside of a namespace, my code is trying to get the members of the EnvDTE.CodeNamespace object but it returns empty, I tried Children too but it was empty too.

http://www.mztools.com/articles/2006/MZ2006009.aspx

My code is generally C# implementation of this code. It struggles at ;

If TypeOf objCodeElement Is EnvDTE.CodeNamespace Then
   colCodeElements = CType(objCodeElement, EnvDTE.CodeNamespace).Members

this function returns empty CodeElements. Any idea how to solve it?

P.S: My extension runs on C++ files.

Edit: Posting the test codes.

#include "stdafx.h"
#include "Header.h"
namespace ns_deneme{
    int zaza::func_deneme(int k)
    {
        a = k;
        return a;
    }
}

int wmain(int argc, wchar_t* argv[])
{
    xaxa a;
    int ba = a.deneme2(5);
}

int xaxa::deneme2(int a){
    return a;
}

Header.h

namespace ns_deneme{
    class zaza{
        private:
            int a;
            int func_deneme(int k);
    };
}

class xaxa{
public:
    int deneme2(int a);
};

Problem is that it does find the functions of wmain and deneme2 at cpp file (header file also not working it gives something like -over range etc.-) but also deneme from cpp file isn't working either.


Solution

  • I recommend a different way to get current function:

        EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
        if (ts == null)
            return;
        EnvDTE.CodeFunction func = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementFunction]
                    as EnvDTE.CodeFunction;
        if (func != null)
          System.Windows.MessageBox.Show(func.FullName);