我正在尝试获取基类的所有属性。我能够获得基类(cls.Parent)并获取基类上的所有方法,但是当迭代方法并获取method.Attributes时,它总是为空。
如何获取基类中方法的属性?
谢谢!
编辑:这是相关代码:
void LoadAPICalls() {
IEnumerable<CodeElement> allClasses = VisualStudioHelper.CodeModel.GetAllCodeElementsOfType(VisualStudioHelper.CurrentProject.CodeModel.CodeElements, vsCMElement.vsCMElementClass, false);
foreach(CodeClass item in allClasses) {
if (!VisualStudioHelper.CodeModel.GetAllBaseClasses(item).Any(c => c.FullName == "System.Web.Http.ApiController"))
continue;
//Write out the class here.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
var className = item.FullName.Substring(item.FullName.LastIndexOf(".") + 1).Replace("Controller", "");
#>export class <#=className#>
{<#+
var curItem = item;
while (true) {
var allMethods = VisualStudioHelper.CodeModel.GetAllMethods(curItem);
foreach(CodeFunction method in allMethods) {
IEnumerable<CodeElement> allAttributes = VisualStudioHelper.CodeModel.GetAllCodeElementsOfType(method.Attributes, vsCMElement.vsCMElementAttribute, true);
foreach (CodeElement attribute in allAttributes)
{
if (attribute.Kind != vsCMElement.vsCMElementAttribute)
continue;
switch(attribute.FullName) {
case "System.Web.Http.HttpPostAttribute":
WriteHttpPost(className, method, sb);
break;
case "System.Web.Http.HttpGetAttribute":
WriteHttpGet(className, method, sb);
break;
case "System.Web.Http.HttpPutAttribute":
WriteHttpPut(className, method, sb);
break;
case "System.Web.Http.HttpDeleteAttribute":
WriteHttpDelete(className, method, sb);
break;
}
}
}
curItem = VisualStudioHelper.CodeModel.GetBaseClass(curItem);
if (curItem == null)
break;
}
#>
}