是否可以列出我们所有的字符串变量名称和值
可以列出实例中的变量名称及其值。
public class Car
{
public string Color;
public string Model;
public string Made;
}
protected void Page_Load(object sender, EventArgs e)
{
//Create new instance
Car MyCar = new Car();
MyCar.Color = "Red";
MyCar.Model = "NISSAN";
MyCar.Made = "Japan";
//SOMETHING HERE
foreach (MyCar Variable in MyCar)
{
Response.Write("<br/>Variable Name"+ "XXX"+ "Variable Value");
}
}
Is is possible to list our the variable names from the instance and it value.
public class Car
{
public string Color;
public string Model;
public string Made;
}
protected void Page_Load(object sender, EventArgs e)
{
//Create new instance
Car MyCar = new Car();
MyCar.Color = "Red";
MyCar.Model = "NISSAN";
MyCar.Made = "Japan";
//SOMETHING HERE
foreach (MyCar Variable in MyCar)
{
Response.Write("<br/>Variable Name"+ "XXX"+ "Variable Value");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样的事情:
Try something like this:
你需要反思来做到这一点。
在这里您可以看到类似的问题: 如何获取类中所有公共变量的列表? (C#)。
基于此,我认为您的情况将通过以下代码解决:
You will need Reflection to do it.
Here you can see a similar question: How do I get a list of all the public variables I have in a Class? (C#).
Based on it, I think your case will be solved by this code:
这可以使用反射来完成。但是,如果您想枚举类中包含的所有内容,您可以简单地使用字典并枚举它。
This can be done using reflection. But if you want to enumerate whatever is contained within your class, you could simply use a dictionary and enumerate that.
像这样的东西:
Something like this: