获取新类/结构的名称(字符串)

时间:2016-08-24 12:16:56

标签: c#

我正在寻找一种方法来获取在C#6下实例化的类(或结构)的名称。例如:

public class Test
{
public int myvalue;

public Test(int _myvalue){ myvalue= _myvalue;}
}

void Init()
{
    public Test test= new Test(1);
}

void main()
{
    string nameNewTest = // here what I need, that must return "test", not "Test"
}

我看到很多主题可以找到类似的类型:GetType()。Name / nameOf / typeof(Class).Name或者类似的东西,但没有一个新变量的名称。

1 个答案:

答案 0 :(得分:0)

您可以使用

string nameNewTest = nameof(Test);

没有C#6

string nameNewTest = typeof(Test).Name;

您可以查看this post的属性/字段