获取当前MethodBase在ASP.NET vNext [英] Get current MethodBase in ASP.NET vNext

查看:163
本文介绍了获取当前MethodBase在ASP.NET vNext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植从常规.NET 4客户端配置文件,以DNX核心5.0的开源库。有相当多的库变化,属性或方​​法被各地移动或完全删除。我已经看过这个答案但它并不在我的工作情况,因为除去方法

I'm porting an open source library from the regular .NET 4 Client Profile to DNX Core 5.0. There are quite a few library changes, with properties or methods being moved around or altogether removed. I have looked at this answer but it doesn't work in my case because the method was removed.

我有一张code的,其中 MethodBase.GetCurrentMethod()被称为问题。这种方法不再API中存在。类似于剩下的唯一方法是:

One of the issue I have a piece of code where MethodBase.GetCurrentMethod() is called. This method no longer exists in the API. The only methods left that are similar are:

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

但我不知道什么是'把手'的。我需要获得MethodBase才能访问其参数,然后处理他们的REST API查询。这是code,它构建对象.NET 4:

But I'm not sure what the 'handle' is. I need to get the MethodBase in order to access its parameters to then process them for a REST API query. This is the code that builds the object in .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;
    }

我可以访问 SearchToString的 MethodBase 对象参数()方法还有什么其他的方式,如果我能'无以轻易通过说 MethodBase 作为参数?

What other way could I have to access the MethodBase object parameters in SearchToString() method if I can't easily pass the said MethodBase as a parameter?

推荐答案

假设方法注释是在类的TestClass ,用

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

这篇关于获取当前MethodBase在ASP.NET vNext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆