开发者

How loop sub classes properties from the main class?

How loop sub classes properties from the main class?

public class GroupA
{
  public string FullName = "", BirthDay = "";
}
public class GroupB
{
  public string Email = "";
}
public class GroupC
{
  public string Phone;
}

public class MainGroup
{
  public GroupA GroupA;
  public GroupB GroupB;
  public GroupC GroupC;
}


    protected void Page_Load(object sender, EventArgs e)
    {

      GroupA  NewGroupA = new GroupA();
              NewGroupA.FullName="TEST MASTER";
              NewGroupA.BirthDay="02/20/1984";
      GroupB  NewGroupB = new GroupB();
              NewGroupB.Email="noreply@test.com"; 
      GroupC  NewGroupC=new GroupC();
              NewGroupC.Phone="555 123 4567";

      //Assign new class instances to the main class
      MainGroup NewMainGroup= new 开发者_Python百科MainGroup();
                NewMainGroup.GroupA=NewGroupA;
                NewMainGroup.GroupB=NewGroupB;                 
                NewMainGroup.GroupC=NewGroupC;

      //Loop through the main class

      foreach (var Group in typeof(MainGroup).GetFields())
      {
        Response.Write("<BR>MainGroupName=  " + Group.Name + " Value=  " + Group.GetValue(NewMainGroup).ToString());
        //PROBLEM IS HERE. Can't loop through the subclass We need to display the GroupA, GroupB, GroupC below.
        foreach (var SubGroup in Group)
        {
          Response.Write("<BR>");
        }


      }
}


If I understand your question right, I think this code is your answer:

foreach (var group in NewMainGroup.GetType().GetFields())
{
    var groupInstance = group.GetValue(NewMainGroup);
    foreach (var subGroup in groupInstance.GetType().GetFields())
    {
        Response.Write("<br />" + subGroup.Name + " = " + subGroup.GetValue(groupInstance));
    }
}


What you should to is store a reference to the group-variable (not just the type). So, if you take your code and do something like this:

foreach (var GroupType in typeof(MainGroup).GetFields())
{
    object Group = GroupType.GetValue(NewMainGroup);
    Response.Write("<BR>MainGroupName=  " + GroupType.Name + " Value=  " + Group.ToString());
    foreach(var SubGroupType in Group.GetType().GetFields()) {
        object SubGroup = SubGroupType.GetValue(Group);
        Response.Write("<BR>SubGroupName= " + SubGroupType.Name + " Value= " + SubGroup.ToString());
    }
}

Haven't tested it, but I think that should about work. At least get you started.

Oh, and by the way, I think I have a better method for you, try this one:

public Dictionary<string, object> GetObjectFields(object obj) {
    var dict = new Dictionary<string, object>();
    var t = obj.GetType();
    foreach(var f in t.GetFields()) {
        dict[f.Name] = f.GetValue(obj);
    }
    return dict;
}

Then you can simply do this:

var groupVars = GetObjectFields(NewMainGroup);
foreach(var gv in groupVars) {
    Response.Write(<BR>MainGroupName=  " + gv.Key + " Value=  " + gv.Value.ToString());
    var subGroupVars = GetObjectFields(gv.Value);
    foreach(var sgv in subGroupVars) {
        Response.Write(<BR>SubGroupName=  " + sgv.Key + " Value=  " + sgv.Value.ToString());
    }
}


First thing first GroupA, GroupB and GroupC are not subclasses of MainGroup. You are using composition and have members which are of GroupA, GroupB and GroupC types.

Not sure why you want to relflect the types when you already know the type you have. You could have used MainGroup.GroupA.Name and so on. May be the code you posted is just an example?

The below method should reflect the basic structure and suit your purpose.

        static String Reflect(Object data)
        {
            StringBuilder stringBuilder = new StringBuilder();

            foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
            {
                if (propertyInfo.PropertyType.IsClass && propertyInfo.PropertyType != typeof(String))
                {
                    stringBuilder.AppendFormat("{0} : {1} {2}", propertyInfo.Name, Environment.NewLine 
                        , Reflect(propertyInfo.GetValue(data, null)));
                }
                else
                {
                   stringBuilder.AppendFormat("{0} = {1}, {2}", propertyInfo.Name,  propertyInfo.GetValue(data, null), Environment.NewLine);
                }
            }

            return stringBuilder.ToString();
        }

usage:

            MainGroup main = new MainGroup
            {
                A = new GroupA { Name = "GroupA", ID = 1 },
                B = new GroupB { Date = DateTime.UtcNow },
                C = new GroupC { HasData = false }
            };

            Reflect(main);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜