是否可以列出我们所有的字符串变量名称和值

发布于 2024-11-06 07:46:41 字数 486 浏览 0 评论 0原文

可以列出实例中的变量名称及其值。

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

胡大本事 2024-11-13 07:46:41

尝试这样的事情:

using System;

class Car
{
    public string Color;
    public string Model;
    public string Made;
}

class Example
{
    static void Main()
    {
        var car = new Car
        {
            Color = "Red",
            Model = "NISSAN",
            Made = "Japan"
        };

        foreach (var field in typeof(Car).GetFields())
        {
            Console.WriteLine("{0}: {1}", field.Name, field.GetValue(car));
        }
    }    
}

Try something like this:

using System;

class Car
{
    public string Color;
    public string Model;
    public string Made;
}

class Example
{
    static void Main()
    {
        var car = new Car
        {
            Color = "Red",
            Model = "NISSAN",
            Made = "Japan"
        };

        foreach (var field in typeof(Car).GetFields())
        {
            Console.WriteLine("{0}: {1}", field.Name, field.GetValue(car));
        }
    }    
}
请持续率性 2024-11-13 07:46:41

你需要反思来做到这一点。
在这里您可以看到类似的问题: 如何获取类中所有公共变量的列表? (C#)

基于此,我认为您的情况将通过以下代码解决:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    FieldInfo[] myFieldInfo;
    Type myType = typeof(Car);

    myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

    string result = @"The String fields of Car class are:";

    for (int i = 0; i < myFieldInfo.Length; i++)
    {
        if (myFieldInfo[i].FieldType == typeof(String))
        {
            result += "\r\n" + myFieldInfo[i].Name;
        }
    }

    MessageBox.Show(result);
}

public class Car
{
    public string Color;
    public string Model;
    public string Made;
}

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:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    FieldInfo[] myFieldInfo;
    Type myType = typeof(Car);

    myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

    string result = @"The String fields of Car class are:";

    for (int i = 0; i < myFieldInfo.Length; i++)
    {
        if (myFieldInfo[i].FieldType == typeof(String))
        {
            result += "\r\n" + myFieldInfo[i].Name;
        }
    }

    MessageBox.Show(result);
}

public class Car
{
    public string Color;
    public string Model;
    public string Made;
}
尛丟丟 2024-11-13 07:46:41

这可以使用反射来完成。但是,如果您想枚举类中包含的所有内容,您可以简单地使用字典并枚举它。

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.

烟燃烟灭 2024-11-13 07:46:41

像这样的东西:

foreach (var prop in typeof(Car).GetProperties())
{
  Response.Write(prop.Name + ": " + prop.GetValue(MyCar, null) ?? "(null)");
}

Something like this:

foreach (var prop in typeof(Car).GetProperties())
{
  Response.Write(prop.Name + ": " + prop.GetValue(MyCar, null) ?? "(null)");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文