在 WCF 中序列化时不包含 IComparable

发布于 2024-08-31 07:44:11 字数 1756 浏览 6 评论 0原文

我有一个在服务器端填写的列表。它是一个“User”列表,它实现了 IComparable。 现在,当 WCF 序列化数据时,我猜它不包括 CompareTo 方法。这是我的对象类:

[DataContract]
public class User : IComparable
{
    private string e164, cn, h323;
    private int id;
    private DateTime lastActive;

    [DataMember]
    public DateTime LastActive
    {
        get { return lastActive; }
        set { laatstActief = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string H323
    {
        get { return h323; }
        set { h323 = value; }
    }
    [DataMember]
    public string Cn
    {
        get { return cn; }
        set { cn = value; }
    }
    [DataMember]
    public string E164
    {
        get { return e164; }
        set { e164 = value; }
    }

    public User()
    {

    }

    public User(string e164, string cn, string h323, DateTime lastActive)
    {
        this.E164 = e164;
        this.Cn = cn;
        this.H323 = h323;
        this.LastActive= lastActive;
    }
    [DataMember]
    public string ToStringExtra
    {
        get
        {
            if (h323 != "/" && h323 != "")
                return h323 + " (" + e164 + ")";
            return e164;
        }
        set { ;}
    }

    public override string ToString()
    {
        if (Cn.Equals("Trunk Line") || Cn.Equals(""))
            if (h323.Equals(""))
                return E164;
            else
                return h323;
        return Cn;
    }

    public int CompareTo(object obj)
    {
        User user = (User)obj;
        return user.LastActive.CompareTo(this.LastActive);
    }
}

是否可以使用 CompareTo 方法来到达客户端? 正如我尝试的那样,放置 [DataMember] 并不是解决方案(我知道......)。

提前致谢。

I have a list i'm filling at server side. It's a list of "User", which implements IComparable.
Now when WCF is serializing the data, i guess it's not including the CompareTo method. This is my Object class :

[DataContract]
public class User : IComparable
{
    private string e164, cn, h323;
    private int id;
    private DateTime lastActive;

    [DataMember]
    public DateTime LastActive
    {
        get { return lastActive; }
        set { laatstActief = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string H323
    {
        get { return h323; }
        set { h323 = value; }
    }
    [DataMember]
    public string Cn
    {
        get { return cn; }
        set { cn = value; }
    }
    [DataMember]
    public string E164
    {
        get { return e164; }
        set { e164 = value; }
    }

    public User()
    {

    }

    public User(string e164, string cn, string h323, DateTime lastActive)
    {
        this.E164 = e164;
        this.Cn = cn;
        this.H323 = h323;
        this.LastActive= lastActive;
    }
    [DataMember]
    public string ToStringExtra
    {
        get
        {
            if (h323 != "/" && h323 != "")
                return h323 + " (" + e164 + ")";
            return e164;
        }
        set { ;}
    }

    public override string ToString()
    {
        if (Cn.Equals("Trunk Line") || Cn.Equals(""))
            if (h323.Equals(""))
                return E164;
            else
                return h323;
        return Cn;
    }

    public int CompareTo(object obj)
    {
        User user = (User)obj;
        return user.LastActive.CompareTo(this.LastActive);
    }
}

Is it possible to get the CompareTo method to reach the client?
Putting [DataMember] isn't the solution as i tried it ( i know...).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深府石板幽径 2024-09-07 07:44:11

您不需要实施比较。

您也可以使用委托对列表进行排序,而无需类中的 icomparable。尝试以下代码。

users.Sort(delegate(User u1, User u2) { return u1.LastActive.CompareTo(u2.LastActive); });

您可以直接在客户端中使用它。

You do not need to implement comparable.

You can use delegates to sort a list as well, without the icomparable in the class. Try following code.

users.Sort(delegate(User u1, User u2) { return u1.LastActive.CompareTo(u2.LastActive); });

You can use this directly in your client.

痴梦一场 2024-09-07 07:44:11

不,CompareTo 是方法而不是成员。

如果您想在客户端复制此操作,请提供一个适应客户端对象并实现 IComparable 的客户端库。

@frogbot 确实有一个有效的建议,但传递对象违背了 SOA 的本质,目标是讨论接口,这就是为什么他们让 NetDataContractSerializer 的使用变得更加困难。

No, CompareTo is a method not a member.

If you want to replicate this on the client side, either provide a client side library that adapts the client object as well as implements IComparable.

@frogbot does have a valid suggestion but passing objects is against the true nature of SOA, the goal is to talk interfaces and this is why they have made it harder to use the NetDataContractSerializer.

2024-09-07 07:44:11

接口(代码)未序列化。您可以考虑切换到NetDataContractSerializer。在这种情况下,类型信息将包含在流中,并且您可以在客户端获取相同的对象(如果包含该类型的程序集位于 AppDomain 中)。

Interfaces (code) are not serialized. You may consider switching to NetDataContractSerializer. In this case type info will be included in the stream and you can obtain the same object on the client (if the assembly containing the type is in AppDomain).

孤独岁月 2024-09-07 07:44:11

因为您的客户端和服务器正在使用相同的技术堆栈(即都使用 .Net),所以让您的客户端代码引用与服务器使用的相同(包含数据对象)的程序集*。然后,您的接口实现将完好无损,两个程序集将使用相同的数据对象定义,而不是服务器使用常规定义,客户端使用作为代理的一部分生成的定义。

共享或“重用”这些程序集是 SO 中已充分讨论的主题。

*这意味着您的数据对象(例如用户)包含在单独的程序集中,这是该程序集的唯一任务。然后你的客户端和你的服务器(webservice)都可以引用它。

Because your client and server are talking the same technology stack (i.e. both are using .Net), have your client side code reference the same (data object containing) assembly as what the server uses*. Then your interface implementations will be intact, both assemblies will be using the same data object definitions, rather than the server using the regular definition and the client using the definition that is generated as part of the proxy.

Sharing or "reusing" these assemblies is a topic that has been well covered on SO.

*this means your data objects like User are contained in a separate assembly, that is the sole task of that assembly. Then both your client and your server (webservice) can reference it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文