Get Access property of a CodeElement

只谈情不闲聊 提交于 2019-12-23 12:37:02

问题


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:


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;
}


来源:https://stackoverflow.com/questions/6014592/get-access-property-of-a-codeelement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!