就像标题说的:反射能给你当前正在执行的方法的名字吗?
我倾向于认为不是,因为海森堡的问题。如何调用一个方法,在不改变当前方法的情况下告诉您当前方法?但我希望有人能证明我错了。
更新:
第2部分:这也可以用于在代码中查找属性吗? 第三部分:演出是什么样的?
最终结果 我学习了MethodBase.GetCurrentMethod()。我还了解到,我不仅可以创建堆栈跟踪,还可以只创建我需要的确切帧。
要在属性中使用它,只需使用. substring(4)来删除'set_'或'get_'。
就像标题说的:反射能给你当前正在执行的方法的名字吗?
我倾向于认为不是,因为海森堡的问题。如何调用一个方法,在不改变当前方法的情况下告诉您当前方法?但我希望有人能证明我错了。
更新:
第2部分:这也可以用于在代码中查找属性吗? 第三部分:演出是什么样的?
最终结果 我学习了MethodBase.GetCurrentMethod()。我还了解到,我不仅可以创建堆栈跟踪,还可以只创建我需要的确切帧。
要在属性中使用它,只需使用. substring(4)来删除'set_'或'get_'。
当前回答
试试这个…
/// <summary>
/// Return the full name of method
/// </summary>
/// <param name="obj">Class that calls this method (use Report(this))</param>
/// <returns></returns>
public string Report(object obj)
{
var reflectedType = new StackTrace().GetFrame(1).GetMethod().ReflectedType;
if (reflectedType == null) return null;
var i = reflectedType.FullName;
var ii = new StackTrace().GetFrame(1).GetMethod().Name;
return string.Concat(i, ".", ii);
}
其他回答
Lex提供的片段有点长,所以我指出了重要的部分,因为没有其他人使用完全相同的技术:
string MethodName = new StackFrame(0).GetMethod().Name;
这将向MethodBase.GetCurrentMethod()返回相同的结果。命名技巧,但它仍然值得指出,因为我可以在它自己的方法中实现一次,使用前一个方法的索引1,并从许多不同的属性调用它。此外,它只返回一帧而不是整个堆栈跟踪:
private string GetPropertyName()
{ //.SubString(4) strips the property prefix (get|set) from the name
return new StackFrame(1).GetMethod().Name.Substring(4);
}
这也是一行代码;)
我认为你应该能够从创建一个StackTrace。或者,正如@edg和@Lars Mæhlum提到的,MethodBase.GetCurrentMethod()
试试这个…
/// <summary>
/// Return the full name of method
/// </summary>
/// <param name="obj">Class that calls this method (use Report(this))</param>
/// <returns></returns>
public string Report(object obj)
{
var reflectedType = new StackTrace().GetFrame(1).GetMethod().ReflectedType;
if (reflectedType == null) return null;
var i = reflectedType.FullName;
var ii = new StackTrace().GetFrame(1).GetMethod().Name;
return string.Concat(i, ".", ii);
}
肯定是的。
如果你想操纵一个对象,我实际上使用这样的函数:
public static T CreateWrapper<T>(Exception innerException, params object[] parameterValues) where T : Exception, new()
{
if (parameterValues == null)
{
parameterValues = new object[0];
}
Exception exception = null;
StringBuilder builder = new StringBuilder();
MethodBase method = new StackFrame(2).GetMethod();
ParameterInfo[] parameters = method.GetParameters();
builder.AppendFormat(CultureInfo.InvariantCulture, ExceptionFormat, new object[] { method.DeclaringType.Name, method.Name });
if ((parameters.Length > 0) || (parameterValues.Length > 0))
{
builder.Append(GetParameterList(parameters, parameterValues));
}
exception = (Exception)Activator.CreateInstance(typeof(T), new object[] { builder.ToString(), innerException });
return (T)exception;
}
这条线:
MethodBase method = new StackFrame(2).GetMethod();
遍历堆栈帧以查找调用方法,然后使用反射获取传递给它的参数信息值,用于通用错误报告函数。要获得当前方法,只需使用当前堆栈框架(1)即可。
正如其他人所说的,当前方法的名称你也可以使用:
MethodBase.GetCurrentMethod()
我更喜欢遍历堆栈,因为如果在内部查看该方法,它只是创建了一个StackCrawlMark。对我来说,直接寻址堆栈似乎更清楚
在4.5之后,你现在可以使用[CallerMemberNameAttribute]作为方法参数的一部分来获取方法名的字符串——这在某些情况下可能会有帮助(但实际上在上面的例子中)
public void Foo ([CallerMemberName] string methodName = null)
这似乎主要是INotifyPropertyChanged支持的解决方案,以前你在你的事件代码中到处都是字符串。
为了同时处理异步和普通的旧方法调用,我这样做了。
在我的应用程序中,它只从异常处理程序调用,所以性能不是一个问题。
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentMethodName()
{
var st = new StackTrace();
var sf = st.GetFrame(1);
string name = sf.GetMethod().Name;
if (name.Equals("MoveNext"))
{
// We're inside an async method
name = sf.GetMethod().ReflectedType.Name
.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries)[0];
}
return name;
}