【问题标题】:Tableadapters Not Marked as serializable?表适配器未标记为可序列化?
【发布时间】:2011-06-23 00:38:42
【问题描述】:

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

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

Type '"".""TableAdapters.""TableAdapter' in Assembly '"", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 未标记为可序列化。

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

[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;

        }

有什么问题?

【问题讨论】:

    标签: c# .net list serialization viewstate


    【解决方案1】:

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

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

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

    编辑:代码示例

    [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; }
    
    }
    

    【讨论】:

    • @user603605 - 添加了一个带有 cmets 的代码 sn-p 来演示一下
    • 我以什么方式“公开展示表格适配器”,我有文本框来收集信息。我希望用户可以选择继续添加到列表或继续。我需要表格适配器,所以最后我可以将列表插入到我的数据库中。
    • @user603605 - 实际上我给出了错误的原因,尽管我上面的回答是“正确的”。我将发布带有链接的编辑。
    • 只读属性的链接不适用。它仅与 XML 序列化有关。公共字段也是如此,XML 序列化程序将处理公共字段和属性,二进制序列化程序将处理所有未标记为 [NonSerialized] 的字段。
    • @erikkallen - 没错,但在此处提供的示例中,OP 将 Example 对象放入 ViewState。那是 XML 序列化而不是二进制。
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2015-11-10
    • 2016-01-18
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多