【问题标题】:EnvDTE and getting constant value from attributeEnvDTE 并从属性中获取常量值
【发布时间】:2015-09-25 12:08:01
【问题描述】:

我有一个带有常量字符串路由的类:

public class Routes
{
   public const string Route1 = "api/customers";
}

还有一个带有 Route 属性的 WebApi 控制器类:

public class CustomerController : ApiController
{
   [Route(Routes.Route1)]
   public IList<Customer> GetCustomers()
   {..}
}

我需要使用 EnvDTE 从属性中获取常量字符串值。

我能够接收 CodeAttribute2 对象,然后接收 CodeAttributeArgument 的值,但它的值只是“Routes.Route1”。

如何导航到 Route 类型,然后导航到 Route1 常量?

【问题讨论】:

    标签: c# visual-studio envdte


    【解决方案1】:

    简短的回答(假设您事先知道该类型位于项目的文件中,而不是位于另一个引用的项目或编译的引用中)是使用 EnvDTE.Project.CodeModel,然后递归地导航代码元素(参见HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in 中的方法)直到找到全名“.Route.Route1”的那个,它应该是一个 EnvDTE.CodeVariable,然后使用 CodeVariable.InitExpression 获取“api/customers”值.

    长答案是:

    How do I get a System.Type from a type name?

    How to get a System.Type from an EnvDTE.CodeTypeRef or EnvDTE.CodeClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多