1

I have a class with constant string routes:

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

And a WebApi controller class with Route attribute:

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

I need to get a string value of constant from attribute using EnvDTE.

I'm able to receive CodeAttribute2 object and then value of CodeAttributeArgument but its Value just "Routes.Route1".

How to navigate to Route type and then to Route1 constant?

Aqua
  • 13
  • 2

1 Answers1

0

The short answer (given that you know in advance that the type resides in a file of your project and not in another referenced project or in a compiled reference) is to use EnvDTE.Project.CodeModel and then navigate the code elements recursively (see the approach in HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in) until you find the one with the FullName "<namespace>.Route.Route1", which should be a EnvDTE.CodeVariable, and then use CodeVariable.InitExpression to get the "api/customers" value.

The long answers are:

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

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

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18