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 には、IList タイプの「Attributes」というプロパティがあることを知っています。
私が解析しているアーカイブは次のようになります。
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")。私は周りを見回しましたが、私を始めるための助けが見つかりませんでした. OPが構文ツリーを解決することができたこのSOの質問に出くわしましたが、彼がしたことが私がやりたいことにどのように関連するかは明確になりませんでした。これに関する専門知識を共有していただきありがとうございます。