c# - 如何即时返回方法的完全限定名称?

标签 c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcMusicStore.Controllers
{
    public class StoreController : Controller
    {
        //
        // GET: /Store/

        public string Index()
        {
            return "MvcMusicsStore.Controllers.StoreController.Index";
        }

    }
}

如何即时返回方法的完全限定名称?

最佳答案

没有任何硬编码?类似的东西?

public string Index()
{
    return GetType().FullName + GetMemberName();
}

static string GetMemberName([CallerMemberName] string memberName = "")
{
    return memberName;
}

或者更漂亮:

public string Index()
{
    return GetMemberName(this);
}

static string GetMemberName(
    object caller, [CallerMemberName] string memberName = "")
{
    return caller.GetType().FullName + "." + memberName;
}

(我使用 static 是因为我假设在实际代码中您希望将方法放在某个实用程序类中)

关于c# - 如何即时返回方法的完全限定名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927873/

相关文章:

c# - 如何避免对类名和属性名使用相同的标识符?

c# - "AssemblyInfo"MSBuild 任务在 Jenkins 构建中失败

javascript - 限制用户上传带有特殊字符的文件

c# - ulong 和 long 之间不可能的比较突然成为可能

c# - 应用程序异常关闭时删除RabbitMQ Queue

c# - NSubstitute 和 AutoFixture 与 Received 相关的问题

c# - 使用空间类型和自动映射器时如何优化 Entity Framework 查询?

c# - 从数据表查询时在 Lambda 表达式中使用 Let

c# - LINQ2SQL 获取随机记录

c# - 无法打印异常字符串,因为 Exception.ToString() 失败