我在尋找一種方式來獲得一個類(或結構)的名稱,這是在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