.NET Core Reflection - 如何在 Core 3 Razor 视图中查找属性 ASP.NET 引用?



我有使用 IViewLocalizer 的本地化资源的 Razor 视图,如下所示:

@inject  Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
...
@Localizer.GetString("Click here to Log in")

正在尝试从预编译的视图中提取所有这些字符串(以构建 PO 文件字典(,所以我想我必须找到注入到我的视图中的 IViewLocalizer 的所有引用,如下所示:


var findAll = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static;
var asm = Assembly.LoadFile("MySite.Web.Views.dll"));
var view = asm.GetType("AspNetCore.Views_Home_Index");
var props = view.GetProperties(findAll).Where(p=>p.PropertyType.FullName == "Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer").ToList();
// I want to find all references that use this this IViewLocalizer Localizer.get()
var getter = props.First().GetGetMethod().MetadataToken; 
// How can I do it?
var allMethods = view.GetMethods(findAll);
foreach(var method in allMethods)
{
// How can I check my methods for usages of that property getter, and in case get the strings?
}

我尝试使用这个答案中的代码,它使用MethodBase.GetMethodBody((。GetILAsByteArray(( 但它不起作用 - 看起来方法public override Task ExecuteAsync()很短,所以看起来我的视图呈现在其他地方。

此外,我不确定我是否应该寻找getter的元数据令牌(IViewLocalizer注入到我的视图中(,或者我是否应该寻找实现IViewLocalizer.GetString(string)IViewLocalizer.GetString(string, params object[])的任何具体方法的用法。

PS:我知道我可以在我的 cshtml 视图上使用正则表达式,只要注入的本地化器遵循一些命名标准。这是B计划,以防我无法弄清楚反射解决方案。

在 ILSpy 的帮助下(后来通过这个答案和这个答案证实了这一点(,我发现异步方法(以及迭代器类(是在表示状态机的嵌套帮助程序类中生成的。这些帮助程序类可以在主类型的 NestedType 属性中找到。

当我在搜索中添加内部类型中的所有方法时,我可以找到对我正在寻找的方法(我的 IViewLocalizer 的获取器(的多次调用。这奏效了:

var allMethods = view.GetMethods(findAll).ToList();
allMethods.AddRange( // also search in methods of nested types
view.GetNestedTypes(findAll).SelectMany(x => x.GetMethods(findAll)));
foreach(var method in allMethods)
{
var usages = ((MethodBase)method).GetMethodUsageOffsets(get);
if (usages.Count() > 0)
Debug.WriteLine($"{method.ReflectedType.FullName}.{method.Name}");
}

我希望在 AspNetCore.Views_Home_Index3.ExecuteAsync(( 中找到的所有调用实际上都分布在内部类型的这些方法中:

AspNetCore.Views_Home_Index3+<ExecuteAsync>d__22.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_0>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_1>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_2>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_3>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_4>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_5>d.MoveNext
AspNetCore.Views_Home_Index3+<<ExecuteAsync>b__22_6>d.MoveNext
... etc... all states from the state machine

使用 Mono.Cecil 变得更加容易,因为我甚至不需要这个GetMethodUsageOffsets扩展名,因为 Cecil 可以轻松解释字节码:

var module = ModuleDefinition.ReadModule(System.IO.Path.Combine(appFolder, "MyApp.Web.Views.dll"));
var type = module.Types.First(x => x.Name.EndsWith("Index3"));
var allInstructions = typeDefinition.Methods
.Union(typeDefinition.NestedTypes.SelectMany(x => x.Methods))
.Where(m => m.HasBody)
.SelectMany(x => x.Body.Instructions).ToList();
var calls = allInstructions.Where(i => i.ToString().Contains(
"callvirt Microsoft.Extensions.Localization.LocalizedString Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer::GetString(System.String)")).ToList();
foreach (var call in calls) // previous is where string parameter is loaded
System.Diagnostics.Debug.WriteLine(i.Previous.ToString());

结果:

IL_020a: ldstr "More Options"
IL_0241: ldstr "Add Text"
IL_15c8: ldstr "Reset"
IL_15ff: ldstr "Save Text Box Settings"
IL_0019: ldstr "None"
...etc

2020-09-23编辑: 我刚刚发现某些方法可以分为多个级别,因此仅搜索第一级 NestedType 不起作用。由于我目前正在使用 Cecil(而不是纯反射(,这是我当前的代码:

// All assemblies that I want to scan
var modules = new List<ModuleDefinition>();
modules.Add(ModuleDefinition.ReadModule(System.IO.Path.Combine(appFolder, "MyProject.Web.Views.dll")));
modules.Add(ModuleDefinition.ReadModule(System.IO.Path.Combine(appFolder, "MyProject.Web.dll")));
// All types in my Assemblies
var allTypes = modules.SelectMany(m => m.Types).ToList();
// Then for each of those types I get all Nested Types:
// Get all nested types
for(int i = 0; I < allTypes.Count(); i++) 
allTypes.AddRange(allTypes[i].NestedTypes); 
// Get all instructions using Cecil
var allInstructions = allTypes.SelectMany(x => x.Methods)
.Where(m => m.HasBody)
.SelectMany(x => x.Body.Instructions).ToList();
var i18nInstructions = allInstructions.Where(i => 
i.ToString().Contains("GetString(System.String)") 
|| i.ToString().Contains("Microsoft.AspNetCore.Mvc.Localization.HtmlLocalizerExtensions::GetHtml(Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer,System.String)")
).ToList();
// This prints all values of "ldstr 'value'" 
// which happen before GetString() or GetHtml()
foreach (var i in i18nInstructions)
System.Diagnostics.Debug.WriteLine(i.Previous.Operand.ToString());

最新更新