使用的System.Reflection获取一个方法的全名
我有一个类,如下所示:使用的System.Reflection获取一个方法的全名
public class MyClass
{
...
protected void MyMethod()
{
...
string myName = System.Reflection.MethodBase.GetCurrentMethod.Name;
...
}
...
}
的myName
值为“的MyMethod”。
有没有一种方法可以使用Reflection来取代myName
的“MyClass.MyMethod”值?
你可以看看MethodBase
的ReflectedType
你GetCurrentMethod
获得,即
MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
string fullMethodName = className + "." + methodName;
正是我一直在寻找的。谢谢! – 2010-06-03 18:16:20
只需注意'ReflectedType'不会像人们所期望的那样解决运行时多态性问题。 – user2864740 2016-11-07 03:43:01
扩展鲁本的,你可以得到全名是这样的:
var info = System.Reflection.MethodBase.GetCurrentMethod();
var result = string.Format(
"{0}.{1}.{2}()",
info.ReflectedType.Namespace,
info.ReflectedType.Name,
info.Name);
您可以将其添加转换为接收MethodBase参数并生成字符串的静态方法。
你可以得到全名是这样的:
var fullMethodName = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName;
并获得与参数完整的方法名称:
var method = System.Reflection.MethodBase.GetCurrentMethod();
var fullName = string.Format("{0}.{1}({2})", method.ReflectedType.FullName, method.Name, string.Join(",", method.GetParameters().Select(o => string.Format("{0} {1}", o.ParameterType, o.Name)).ToArray()));
+1绝对是最有用的解决方案。包含FullName有助于在您的代码库在不同名称空间中重复类名时查明类。并且包含参数有助于区分重载的方法。 – 2015-02-05 16:18:24
感谢以上的职位,他们帮助我建立一个强大的MVC 4 HTMLHelpers的类型绑定系统如下。
public static MvcHtmlString StrongTypeBinder(this HtmlHelper htmlhelper, Expression<Func<object, string>> SomeLambda)
{
var body = SomeLambda.Body;
var propertyName = ((PropertyInfo)((MemberExpression)body).Member).Name;
HtmlString = @"
<input type='text' name='@Id' id='@Id'/>
";
HtmlString = HtmlString.Replace("@Id", propertyName);
var finalstring = new MvcHtmlString(HtmlString);
return finalstring;
}
在任何CSHTML查看使用上面的代码:
@Html.StrongTypeBinder(p=>Model.SelectedDate)
这让我在一个视图模型的任何属性绑定到我想要的任何HTML元素类型。在上面的示例中,我绑定了用户选择后回发的所选数据的名称字段。发布后的视图模型会自动显示选定的值。
在C#6你可以用nameof
:
string myName = nameof(MyMethod);
是的,但是只返回名称,而不是全名。在给出的例子中,它返回字符串“MyMethod”而不是“MyClass.MyMethod”。 – Matt 2016-05-30 12:00:13
我想这些天,最好这样做:
string fullMethodName = $"{typeof(MyClass).FullName}.{nameof(MyMethod)}";
一直在寻找这个,所以我可以登录合格的名字,这是我最终采用的方法。仅仅因为它很小,很简单,并且做我需要的东西。 – onesixtyfourth 2018-03-06 07:57:26
异步方法中运行时,您将有问题。以下是如何解决这个问题:
如果您需要完全限定类名,你将不得不使用的DeclaringType.FullName
代替DeclaringType.Name
此代码不能用于匿名或lambda方法很好地工作。
static string GetMethodContextName() {
var name = new StackTrace().GetFrame(1).GetMethod().GetMethodContextName();
}
static string GetMethodContextName(this MethodBase method) {
if (method.DeclaringType.GetInterfaces().Any(i => i == typeof(IAsyncStateMachine))) {
var generatedType = method.DeclaringType;
var originalType = generatedType.DeclaringType;
var foundMethod = originalType.GetMethods(Instance | Static | Public | NonPublic | DeclaredOnly)
.Single(m => m.GetCustomAttribute<AsyncStateMachineAttribute>()?.StateMachineType == generatedType);
return foundMethod.DeclaringType.Name + "." + foundMethod.Name;
} else {
return method.DeclaringType.Name + "." + method.Name;
}
}
下面是一个例子用法:
class Program {
static void Main(string[] args) {
// outputs Program.Main
Console.WriteLine(GetMethodContextName());
}
}
更正:应该System.Reflection.MethodBase.GetCurrentMethod()名称 – aads 2013-09-26 06:33:05