【问题标题】:Get full name of parameter (namespace, class, method and name) c#获取参数的全名(命名空间、类、方法和名称)c#
【发布时间】:2019-06-26 14:59:54
【问题描述】:

如何获取参数或变量的命名空间、类、方法和名称。例如:

namespace TheNamespace
{
    class Theclass
    {
        void Thing()
        {
            string thevariable = "";
            string b = thevariable.GetFullName();
            // b would be equal to TheNamespace.Theclass.Thing.thevariable
        }
    }

}

我该怎么做

【问题讨论】:

    标签: c# string class reflection namespaces


    【解决方案1】:

    参数和变量没有命名空间/类名,因此要获得您正在寻找的字符串,您只需结合Can you use reflection to find the name of the currently executing method?get name of a variable or parameter

    string b = 
           System.Reflection.MethodBase.GetCurrentMethod().Name + "." + 
           nameof(thevariable);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多