English 中文(简体)
数据合同例外。 不能序列化
原标题:Datacontract exception. Cannot be serialized
  • 时间:2012-04-09 17:28:12
  •  标签:
  • c#
  • .net
  • wcf

我有以下WCF数据:

[DataContract]
public class Occupant
{
    private string _Name;
    private string _Email;
    private string _Organization;
    private string _Badge;

    public Occupant(string name, string badge, string organization)
    {
        Name = name;
        Badge = badge;
        Organization = organization;
    }

    public Occupant(string name, string badge)
    {
        Value = name;
        Key = badge;
    }

    [DataMember]
    public string Key
    {
        get { return _Name; }
        set { _Name = value; }
    }

    [DataMember]
    public string Value
    {
        get { return _Badge; }
        set { _Badge = value; }
    }

    [DataMember]
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    [DataMember]
    public string Email
    {
        get { return _Email; }
        set { _Email = value; }
    }

    [DataMember]
    public string Organization
    {
        get { return _Organization; }
        set { _Organization = value; }
    }

    [DataMember]
    public string Badge
    {
        get { return _Badge; }
        set { _Badge = value; }
    }
}

当我试图通过网络浏览器(设在国际空间法研究所)获得这一服务时,我正在发现这一错误:

系统:MyNamespace。 占领者不能序列化。 考虑用数据记录Attribute属性加以标识,并标明你希望以数据成员Attribute属性分类的所有成员。 如果是收集,则考虑将其与收集、记录和记录联系起来。

我的一种方法是,将<代码>List > > 类型<编码>> >(使用)。 这是否会造成这种情况?

最佳回答

Because you have provided one or more initializing constructors, you will also need to add a parameterless (default) constructor.

i.e. 阁下:

[DataContract]
public class Occupant
{
    // *** Needed only for Serialization
    public Occupant() {}
    ...

这是因为default Constructionor在上消失。 当你增加一个明确的构造者时。

[问题与返回<代码>List<Occupant>的方法有关,因为方法按序排列。]

问题回答

添加一个空洞的构体。 通常会抵消序列器。

你们需要一个没有参数的构造者。 我从未计划实际使用地雷,因此,我增加了英特尔利Sense的概要,并提出一个暂时的例外,以防使用。

/// <summary>
/// parameterless default constructor only for serialization
/// </summary>
public MyClass() { throw new NotImplementedException("parameterless default constructor only for serialization"); }

您应在数据合同类别中添加一个空置的参数构造。

When you have NO setter by default, do these steps:

  1. Add parametreless constructor;
  2. And add private set at least.

这有助于我。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签