jeudi 28 mai 2015

How to get all values in fields static c# class

My main looks like this

    static void Main(string[] args)
    {
        Type t = typeof(foo);

        foreach (var p in t.GetFields(BindingFlags.Static | BindingFlags.Public))
        {
            Console.WriteLine(p.Name + " " + p.GetValue(null).ToString());
            if (p.Name.Equals("temp4"))
            {
                //do something 
            }
        }

        Console.ReadKey();
    }

and all it does now is grab the values in each field, but one of my fields has values it can't grab and I was wondering how you grab those values.

This is what foo looks like

public static class foo
{
      public static readonly string temp1 = new string("temp1");
      public static readonly string temp2 = new string("temp2");
      public static readonly string temp3 = new string("temp3");

      public static readonly OtherClass temp4 = new OtherClass(
                                                               "blah", 
                                                                42, 
                                                                new[]{ 
                                                                       new OtherOtherClass("test"),
                                                                       new OtherOtherClass("test2")
                                                                });
 }

Ideally I want to get all the class fields in OtherClass temp4, so that would be "blah", 42, and an array of OtherOtherClasses which also have values.

My main can only grab values for my strings but not for OtherClass. How can I grab the values inside of OtherClass or even OtherOtherClass?





Aucun commentaire:

Enregistrer un commentaire