asp.net - 在 ASP.NET vNext 中获取当前的 MethodBase

标签 asp.net asp.net-core coreclr

我正在将一个开源库从常规 .NET 4 客户端配置文件移植到 DNX Core 5.0。有相当多的库更改,属性或方法被移动或完全删除。我看过this answer但它在我的情况下不起作用,因为该方法已被删除。

其中一个问题是我有一段代码调用了 MethodBase.GetCurrentMethod()。 API 中不再存在此方法。唯一相似的方法是:

public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle);
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType);

但我不确定“句柄”是什么。我需要获取 MethodBase 才能访问其参数,然后处理它们以进行 REST API 查询。这是在 .NET 4 中构建对象的代码:

public static Annotation Annotation(string query = null, string text = null, string type = null, string name = null, string entity = null, int limit = 25, int offset = 0)
    {
      var search = Help.SearchToString(MethodBase.GetCurrentMethod(), query, text, type, name, entity);
      return Help.Find<Annotation>(search, limit, offset, "annotation");
    }

然后在这里使用:

public static string SearchToString(MethodBase m, params object[] search)
    {
      var paras = m.GetParameters();
      var result = string.Empty;

      for (var i = 0; i < search.Length; i++)
      {
        if (search[i] != null)
        {
          if (i == 0)
          {
            result += search[i] + "%20AND%20";
          }
          else
          {
            result += paras[i].Name.ToLower() + ":" + search[i] + "%20AND%20";
          }         
        }      
      }

      return result.LastIndexOf("%20AND%20", StringComparison.Ordinal) > 0
        ? result.Substring(0, result.LastIndexOf("%20AND%20", StringComparison.Ordinal))
        : result;
    }

如果我不能轻松地传递所述MethodBase,我还有什么其他方法可以访问SearchToString() 方法中的MethodBase 对象参数> 作为参数?

最佳答案

假设方法Annotation在类TestClass中,使用

typeof(TestClass).GetMethod(nameof(Annotation))

关于asp.net - 在 ASP.NET vNext 中获取当前的 MethodBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567941/

相关文章:

asp.net - Azure 网站上的 REST API 上的 400.0 错误请求

c# - 如何让客户机上的程序从 ASP.NET 页面运行?

c# - 我在 ASP.net core MVC 和 Azure iot hub 中遇到错误

entity-framework - 在 ASP.NET 5 和 EF7 (beta 7) 中使用现有数据库

c# - 数据保护 key 未保留到 Azure

asp.net-core - ASP.NET Core 应用程序进入 IdentityServer3 身份验证的无限循环

c# - 我如何在新的 coreclr 世界中模拟对象?

c# - 在 CoreCLR 中运行 F#

C# 加载非托管 DLL : big performance difference between console application and webapp on IIS

c# - 优化 GridView