NRefactory Resolve Attribute

128 Views Asked by At

I would like to resolve the attributes that are associated to methods in a class using NRefactory.

I create a syntax tree from a cs file. Then I go on to find the only type that I have in the file. Then I find the methods that are associated to the file.

var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();
SyntaxTree tree = parser.Parse(line, "demo.cs");        
...
CSharpUnresolvedFile file = tree.ToTypeSystem();

foreach (IUnresolvedTypeDefinition type in file.TopLevelTypeDefinitions)
{
    foreach (IUnresolvedMethod method in type.Methods)
    {
    }
}

I know that IUnresolvedMethod has a property called "Attributes" which is of type IList.

The archive that I am parsing looks like this:

namespace DynamicsFieldsSite.Controllers.FundRaising
{
    /// <summary>
    /// Controlador API de Acciones de Cuenta
    /// </summary>
    public class AccountActionsApiController : ApiController
    {
        private const string fullControllerPath = "api/FundRaising/AccountActionsApi";
        private const string actionKeyPath = "AccountActionsApi";

        private AccountActionManagement _accountActionManage;
        private SystemAppComponent _systemAppComp;

        /// <summary>
        /// Inicializa una nueva instancia de la clase <see cref="AccountActionsApiController" />.
        /// </summary>
        public AccountActionsApiController()
        {
            _accountActionManage = new AccountActionManagement(this.GenerateInformation());
            _systemAppComp = new SystemAppComponent();
        }

        /// <summary>
        /// Obtiene un listado de modelos vista item de Acciones de Cuenta.
        /// </summary>
        /// <returns>Listado de modelos vista item de Acciones de Cuenta.</returns>
        [WebApiAuthorize(ActionKey = actionKeyPath + "-01")]
        [Route(fullControllerPath + "/Get")]
        public List<AccountActionItemViewModel> GetAllAccountActions()
        {
            return _accountActionManage.GetAllAccountActions();
        }
    }
}

I would like to resolve the attributes so I could get the string within the attribute tags. (Ex. fullControllerPath + "/Get"). I have looked around but I have not find help to get me started. I came across this SO question in which the OP managed to solve the syntax tree but it did not become clear to me how what he did could relate to what I want to do. Thanks for your share of expertise on this one.

0

There are 0 best solutions below