7

以下代码指定了从基类“TestBase”派生的类型“MyBase64Binary”

using System;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;

namespace Test
{
    public class TestBase
    {
        public TestBase()
        {
        }
    }

    [XmlType(TypeName = "base64Binary"), Serializable]
    public partial class MyBase64Binary : TestBase
    {
        [System.Xml.Serialization.XmlTextAttribute(DataType = "base64Binary")]
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public Byte[] __Value;

        [XmlIgnore]
        public Byte[] Value
        { 
            get { return __Value; }
            set { __Value = value; }
        }

        public MyBase64Binary()
        {
        }

    }
}

如果我尝试像这样创建一个 XmlSerializer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer s = new XmlSerializer(typeof(Test.MyBase64Binary));
        }
    }
}

从这个然后我得到一个 InvalidOperationException 错误:

{"There was an error reflecting type 'Test.MyBase64Binary'."}

内部异常告诉我以下内容:

{"Cannot serialize object of type 'Test.MyBase64Binary'. Consider changing type of XmlText member 'Test.MyBase64Binary.__Value' from System.Byte[] to string or string array."}

如果我不是从“TestBase”类派生的,那么一切正常。

我没有得到解决方案。请帮忙。
怎么了?

来自德国的问候
一月

4

2 回答 2

4

If you change the XmlTextAttribute to XmlAttribute or XmlElement it should be ok. Since you were trying to use the XmlTextAttribute, it assumed it would be some sort of string. If you want an actual byte array serialized, try the XmlAttribute or XmlElement

于 2010-03-31T11:36:47.217 回答
-1

添加[Serializable]到您的基类有帮助吗?我会考虑确保您的基类也得到适当的装饰。不过,我不知道这是否会有所帮助。

于 2010-03-31T11:33:09.073 回答