Tableadapters未标记为可序列化?

时间:2011-02-07 20:58:08

标签: c# .net list serialization viewstate

我在数据库中有2个表,然后在visual studio(数据集)中使用数据模型,然后使用2个类来存储这2个表的方法和属性。

我想将从webform收集的信息存储到列表中,但出于某种原因,在尝试将列表添加到状态视图时,我收到此错误:

键入“”“。”“TableAdapters。”“AssemblyAdapter”在Assembly'“”,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。

我已经将该类标记为可序列化但现在是tableadapters?这是我的代码:

[System.ComponentModel.DataObject]
    [Serializable]
    public class Example
    {
        int _example1 = new int();
        string _example2;
        string _example3; 
        decimal _example4 = new decimal();

        public int example1
        {
            get { return _example1; }
            set { _example1 = value; }
        }

        public string example2
        {
            get { return _example2; }
            set { _example2 = value; }
        }

        public string example3
        {
            get { return _example3; }
            set { _example3 = value; }
        }

        public decimal example4
        {
            get { return _example4; }
            set { _example4 = value; }
        }

        private tblTestTableAdapter _testAdapter = null;
        protected tblTestTableAdapter Adapter
        {
            get
            {
                if (_testAdapter == null)
                    _testAdapter = new tblTestTableAdapter();

                return _testAdapter;
            }
        }

网络表单:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
            else
            {
                example = (List<Example>)ViewState["Examples"];
            }
        }

        private List<Example> example;
        public List<Example> GetExample()
        {
            return example;
        } 

        protected void btnRow_Click(object sender, EventArgs e)
        {
            example = new List<Example>(); 
            Example e = new Example();
            e.example1 = Convert.ToInt32(txtE1.Text);
            c.example2 = txtE2.Text;
            c.example3 = txtE3.Text;
            c.example4 = Convert.ToDecimal(txtE4.Text);

            example.Add(e);
            ViewState["Examples"] = example;

            btnRow.Enabled = false;

        }

有什么问题?

1 个答案:

答案 0 :(得分:0)

将类标记为Serializable时,外部公开的每个类和依赖对象类都必须标记为Serializable。这是因为执行序列化的任何进程都会尝试正确地序列化每个公共元素。

仅供参考,tableadapters不打算公开公开,因为它们公开功能而不是属性和字段。功能不通过串行连接传输。我建议您在示例中删除适配器的公共属性。

编辑2:
重新阅读代码并查找序列化属性保护级别的文档后,我在这里遇到了this link that describes the real problem。你不能序列化readonly属性(我完全忘了这个),你的tableadapter属性是readonly。为它提供一套,它应该开始运作。

编辑:代码示例

[Serializable]
public class MySerializableClass
{
    public MySerializableClass()
    {

    }

    // This string serializes ok
    public string MyStringProperty { get; set; } 

    // Because this property is public in scope it must be serializable
    // because it will be translated at a public scope. This will throw
    // an exception
    public myNonSerializableClass NotSerializableObject { get; set; }   

    // Because this property is private in scope, it will not be included
    // in any serialization calls, so it will not throw an exception, but
    // it will also not be available in whatever remote class calls it.
    private myNonSerializableClass SerializableObject { get; set; } 

    // Because this property object is serializable in code it will be
    // ok to make it public because it will natively serialize itself
    public MyOtherSerializableClass OtherSerializableObject { get; set; }

}