获取名称(字符串)
问题描述:
我在寻找一种方式来获得一个类(或结构)的名称,这是在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或类似的东西,但没有一个用于新变量的名称。
因为它不是实例的名称,它只是一个变量名称。也许你的意思是'nameof(test)'(C#6),但是由于你的代码片段不是有效的C#,很难告诉它在哪里使用它。 –
你的意思是'nameof(Test)'? –
这甚至不编译(忽略'main')。请尝试更正您的示例并准确解释您想实现的目标。 – germi