2015-09-17 52 views
1

恆定值,我有一個常量字符串路線類:EnvDTE並獲得從屬性

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

並與路由屬性的WebAPI控制器類:

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

我需要得到的字符串值來自屬性的常數使用EnvDTE

我能夠得到CodeAttribute2對象,然後CodeAttributeArgument但其價值只是「Routes.Route1」值。

如何定位至路線類型,然後到ROUTE1不變?

回答

0

簡短的回答(假定您事先知道類型駐留在您的項目的文件中,而不是在另一個引用的項目或編譯的引用中)是使用EnvDTE.Project.CodeModel,然後導航代碼元素直到找到FullName爲「<命名空間> .Route.Route1」(應該是EnvDTE.CodeVariable),然後使用CodeVariable.InitExpression獲取「api/customers」值的那個方法遞歸(請參閱HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in中的方法)。

長的答案是:

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

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

+0

太謝謝你了! – Aqua