【发布时间】:2016-08-24 12:16:55
【问题描述】:
我正在寻找一种方法来获取在 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),但由于您的代码 sn-p 不是有效的 c#,因此很难说出在哪里使用它。 -
你的意思是
nameof(Test)吗? -
这甚至不能编译(忽略
main)。请尝试更正您的示例并准确解释您想要实现的目标。 -
请清理您的问题,发布minimal reproducible example 并准确说明您想要什么以及为什么。
-
你是指实例名“test”还是类名“Test”? (因为,如果你想获得“test”/“Test”,你知道
Test test = new Test()是一个非常糟糕的例子)
标签: c#