DataTable多线程写不安全

如下代码:

class test
{
    private DataTable ftable;

    public test()
    {
        ftable = new DataTable();

        DataColumn col = new DataColumn("COL_1", typeof(int));
        ftable.Columns.Add(col);
        col = new DataColumn("COL_2", typeof(int));
        ftable.Columns.Add(col);

        for (int i = 1; i < 100; i++)
        {
            DataRow row = ftable.NewRow();
            row["COL_1"] = i;
            row["COL_2"] = i * 2;
            ftable.Rows.Add(row);
        }
    }

    public void AddColTest()
    {
        DataColumn col = new DataColumn("COL_Test", typeof(int));
        ftable.Columns.Add(col);

        Parallel.For(0, ftable.Rows.Count, (index) =>
            {
                Thread.Sleep(500);

                DataRow row = ftable.Rows[index];
                row["COL_Test"] = Convert.ToInt32(row["COL_1"]) + Convert.ToInt32(row["COL_2"]);
            });
    }

    public void ShowTable()
    {
        for(int index = 0; index < ftable.Rows.Count; ++index)
        {
            DataRow row = ftable.Rows[index];
            Console.WriteLine(String.Format("{0} \t\t {1} \t\t {2}", row["COL_1"], row["COL_2"], row["COL_Test"]));
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        test t = new test();
        t.AddColTest();

        t.ShowTable();
        Console.ReadKey();
    }
}

我本来以为虽然DataTable不是线程安全的… 但是我并没有对任何一个数据做增删…我只是修改了现有单元格的值…理应不会出现任何问题…DataTable应该是已经分配好的内存…

JSON.NET序列化继承对象

之前使用JSON.NET简单序列化了对象…

现在来读序列化文件…

static void Main(string[] args)
{
    List<BaseTest> baselist = new List<BaseTest>();

    childTest ct = new childTest();
    ct.n = 0;
    ct.f = 1.234f;
    baselist.Add(ct);

    OtherTest ot = new OtherTest();
    ot.n = 0;
    ot.strList.Add("ot1");
    ot.strList.Add("ot2");
    baselist.Add(ot);

    try
    {
        string str = Newtonsoft.Json.JsonConvert.SerializeObject(baselist);
        Console.WriteLine(str);
        List<BaseTest> p = (List<BaseTest>)Newtonsoft.Json.JsonConvert.DeserializeObject<List<BaseTest>>(str);
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.Assert(false);
    }
    Console.ReadKey();
}

结果发现,读取道德序列化,全是基类的信息…

JSON.NET简单序列化

Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库

class BaseTest
{
    public int n;

    public BaseTest()
    {
        n = 0;
    }
}

static void Main(string[] args)
{
    BaseTest bt = new BaseTest();
    bt.n = 1;
    Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(bt));

    Console.ReadKey();
}

输出

{"n":1}

当类有继承关系的时候…

class BaseTest
{
    public int n;

    public BaseTest()
    {
        n = 0;
    }
}

class childTest : BaseTest
{
    public float f;

    public childTest()
        : base()
    {
        f = 1.23f;
    }
}

class OtherTest : BaseTest
{
    public List<string> strList;

    public OtherTest()
        : base()
    {
        strList = new List<string>();
    }
}

static void Main(string[] args)
{
    List<BaseTest> baselist = new List<BaseTest>();

    childTest ct = new childTest();
    ct.n = 0;
    ct.f = 1.234f;
    baselist.Add(ct);

    OtherTest ot = new OtherTest();
    ot.n = 0;
    ot.strList.Add("ot1");
    ot.strList.Add("ot2");
    baselist.Add(ot);

    try
    {
        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(baselist));      
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.Assert(false);
    }
    Console.ReadKey();
}

输出