C# - Class (Instance/object) to List<string>

I have a instance CharObject, which holds several instances of Stat. Is there a way for me to dynamically generate a List from an instance of CharObject? I need to get all the instances of Stat into a List so that I can send them to and from the database. I use a series of text splits and such down the way which all function properly with arrays.

I just need to be able to go ham on them somewhere down the line with text splits and converting them to and from arrays. Need filter power basically. Thanks.

Note: The application requires this flexibility, non-flexible arrays or objects will not do.

CharObject.cs

using UnityEngine;
using System.Collections;
using System;

public class CharObject
{
	private Stat _hp = new Stat();
	private Stat _Src = new Stat();
	private Stat _Sta = new Stat();
	private Stat _Spd = new Stat();
	
	public Stat hp
    {
        set { _hp = value; }
        get { return _hp; }
    }
	
	public Stat Src
    {
        set { _Src = value; }
        get { return _Src; }
    }
	
	public Stat Sta
    {
        set { _Sta = value; }
        get { return _Sta; }
    }
	
	public Stat Spd
    {
        set { _Spd = value; }
        get { return _Spd; }
    }
}

Stat.cs

using UnityEngine;
using System.Collections;

public class Stat
{
	private int _current;
	private int _max;
	private int _temp;
	
	public int current
    {
        set { _current = value; }
        get { return _current; }
    }
	
	public int max
    {
        set { _max = value; }
        get { return _max; }
    }
	
	public int temp
    {
        set { _temp = value; }
        get { return _temp; }
    }
}

Like @jacek-wesolowski mentioned in the comment above a generic .NET / Mono List would do the job :wink:

You need of course to include this namespace:

using System.Collections.Generics;

Then you can use the list like that:

private List<CharObject> _Chars = new List<CharObject>();

// [...]
// Add an object to the list:
CharObject char = new CharObject();
_Char.Add(char);

edit
Sorry i just realized you want the Stats into an array… hold on…

Ok just add this property to your CharObject class:

public Dictionary<string,Stat> AllStats
{
    get
    {
        var result = new Dictionary<string,Stat>();
        System.Type T = GetType();
        FieldInfo[] fields = T.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (var f in fields)
        {
            if (f.FieldType == typeof(Stat))
            {
                Stat S = (Stat)f.GetValue(this);
                result.Add(f.Name,S);
            }
        }
        return result;
    }
}

It will return a dictionary that contains all private fieldnames of type Stat along with the current value (the Stat object).

You need those additional namespaces:

using System.Collections.Generics;
using System.Reflection;

This is my test:

CharObject O = new CharObject();
O.hp.current = 100;
foreach (var S in O.AllStats)
{
    Debug.Log("Stat: " + S.Key + " = "+ S.Value.current);
}

which prints 4 lines:

Stat: _hp = 100
Stat: _Src = 0
Stat: _Sta = 0
Stat: _Spd = 0