简体   繁体   English

获取参数的全名(命名空间,类,方法和名称)C#

[英]Get full name of parameter (namespace, class, method and name) c#

How would I get the namespace, class, method and name of a parameter or variable. 我如何获得名称空间,类,方法和参数或变量的名称。 For example: 例如:

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

}

How would I do this 我该怎么做

Parameters and variables don't have namespace/class name so to get string you are looking for you simply combine Can you use reflection to find the name of the currently executing method? 参数和变量没有名称空间/类名称,因此只需组合即可获取要查找的字符串? 可以使用反射来查找当前正在执行的方法的名称吗? and get name of a variable or parameter : 获取变量或参数的名称

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM