[Unity3D]資料表的使用

摘要:[Unity3D]資料表的使用

前言:在Unity3D裡面想要使用DataTable大致上是都沒什麼問題,不過因為如果要轉到Iphone或是Android(還沒確認)也許會產生問題,

            因為無法將System.Data.DLL檔案匯入到手機裡導致某些製作上用起來很痛苦,所以就想說做一個類似DataTable的類別來呼叫使用

 

 


public class DataRow : Dictionary
{
    public new object this[string column]
    {
        get
        {
            if (ContainsKey(column))
            {
                
                return base[column];
            }

            return null;
        }
        set
        {
            if (ContainsKey(column))
            {
                base[column] = value;
            }
            else
            {
                Add(column, value);
            }
        }
    }

     public new object this[int columnindex]
    {
        get
        {
            if (this.ContainIndex(columnindex))
            {
                return base[this.BaseIndex(columnindex)];
            }

            return null;
        }
        set
        {
            if (this.ContainIndex(columnindex))
            {
               base[this.BaseIndex(columnindex)] = value;
            }
            else
            {
                Add(this.BaseIndex(columnindex), value);
            }
        }
    }
}


public class PuwuDataTable
{
    public PuwuDataTable()
    {
        Columns = new List();
        Rows = new List();
    }

    public List Columns { get; set; }
    public List Rows { get; set; }

    public DataRow this[int row]
    {
        get
        {
            return Rows[row];
        }
    }

    public void AddRow(params object[] values)
    {
        if (values.Length != Columns.Count)
        {
            throw new IndexOutOfRangeException("The number of values in the row must match the number of column");
        }

        var row = new DataRow();
        for (int i = 0; i < values.Length; i++)
        {
            row[Columns[i]] = values[i];
        }

        Rows.Add(row);
    }

}
static public class SyntaxExtend
{

    public static bool ContainIndex(this Dictionary Dict, int index)
    {
        bool IsContain=false;
        if (Dict.Count > index)
        {
            IsContain = true;
        }
        return IsContain;
    }
    public static string BaseIndex(this Dictionary Dict, int index)
    {
        string KeyName = string.Empty;
        if (Dict.Count > index)
        {
            int i = 0;
            foreach (KeyValuePair kvp in Dict)
            {
                if (i == index)
                {
                    KeyName = kvp.Key;
                    break;
                }
                i++;
            }
        }
        else
        { 
        
        }
        return KeyName;
    }
}