Get Access property of a CodeElement

2019-06-24 15:01发布

I'm writing an Add-in for VS 2010. Can't find answer for a question - How can i get the Access property of a CodeElement if it has that one.

I was trying reflection, but no results. Ex. CodeElement is a class method

public void GetAccess (CodeElement codeElement)

{

      object code = codeElement;
      Type t = code.GetType();
      t.GetProperty("Access") = vsCMAccess.vsCMAccessPublic;

}

But it doesnt work..

Help, please!

1条回答
我想做一个坏孩纸
2楼-- · 2019-06-24 15:38

Access is only available on some types of CodeElements, so you'll need to check for the type of CodeElement you have, cast to the specific type and then retrieve the property.

Example:

if (codeElement.Kind == vsCMElementFunction)
{
    return ((CodeFunction)codeElement).Access;
}
else if (codeElement.Kind == vsCMElementProperty)
{
    return ((CodeProperty)codeElement).Access;
}
查看更多
登录 后发表回答