2

我想使用 NRefactory 解析与类中的方法关联的属性。

我从一个 cs 文件创建一个语法树。然后我继续查找文件中唯一的类型。然后我找到与文件关联的方法。

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)
    {
    }
}

我知道 IUnresolvedMethod 有一个名为“Attributes”的属性,它的类型为 IList。

我正在解析的存档如下所示:

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

我想解析属性,以便我可以在属性标签中获取字符串。(例如 fullControllerPath + "/Get")。我环顾四周,但没有找到帮助我开始的方法。我遇到了这个SO 问题,其中 OP 设法解决了语法树,但我并不清楚他所做的与我想做的事情有何关联。感谢您分享这方面的专业知识。

4

0 回答 0