2015-09-15 29 views
3

我将常规.NET 4客户端配置文件中的开放源代码库移植到DNX Core 5.0。有很多库更改,属性或方法被移动或完全移除。我查看了this answer,但由于该方法已被删除,所以在我的情况下无效。在ASP.NET vNext中获取当前MethodBase

其中一个问题我有一段代码,其中MethodBase.GetCurrentMethod()被调用。该方法不再存在于API中。剩下的唯一方法是:

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

但我不确定'handle'是什么。我需要获取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; 
    } 

我能有访问SearchToString()方法如果MethodBase对象参数有什么其他办法我不能轻易通过MethodBase作为参数吗?

+0

嗨@Astaar,你有没有发现一个解决方案? 我有一个.net核心应用程序的相同问题,不能再这样做:MethodBase.GetCurrentMethod(); – Thomas

回答

3

假设方法Annotation是班上TestClass,使用

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

这不会以完全相同的方式工作,并在多个方法共享相同名称时产生异常。 – Jack