C#中的方法内捕获信息

Capture information within a method in C #

我正在尝试从 C# MVC 5 中的方法中的变量获取信息。

例如,我有这样的方法,想获取下面示例中变量 "dt" 中使用的参数。

public static JsonResult NamesDropDownListTest()
{
    var dt = MyClass.QueryStoreProcedureDataTableTest(DataWareHouse,"gpkTest.procedure_type_docs");

    return new JsonResult
    {
        Data = dt,
        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
        MaxJsonLength = int.MaxValue
    };
}

我正在尝试通过我在论坛中看到的这段代码:

        var asm = Assembly.Load("MyAssemblyNameProject");

    var listAsm = asm.GetTypes()
            .SelectMany(type => type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public))
            .Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
            .Select(x => new { Classe = x.DeclaringType.Name, Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = string.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))), Parameter = string.Join(",", x.GetParameters().Select(s => new { atributo = s.ParameterType.FullName, nome = s.Name }).ToList()), teste = string.Join(",", x.Attributes) })
            .OrderBy(x => x.Classe).ThenBy(x => x.Action).ToList();

我想带上这些信息,例如方法名称、操作以及发现哪些方法包含这些变量我需要这些参数的名称。 我希望我说得很清楚。

提前谢谢大家。

无法通过反射获取有关局部变量和方法调用的信息。您必须使用某种反编译器或静态分析工具来找出代码中实际使用方法的位置。