添加网络引用将自定义类设置为空

发布于 2024-11-04 02:46:17 字数 1923 浏览 6 评论 0原文

我的问题是我有这个类:

[SerializableAttribute]
[SoapInclude(typeof(PairColumnValue))]
[XmlInclude(typeof(PairColumnValue))]
public class PairColumnValue:IPairColumnValue
{
    public string ColumnName
    {
        get { return data.Element("ColumnName").Value; }
    }
    public string Value
    {
        get { return data.Element("Value").Value; }
    }


    private XElement data;

    public PairColumnValue()
    {
        data = new XElement("Data", new XElement("ColumnName", ""), new XElement(("Value"), ""));
    }

    public PairColumnValue(string columnName, string value)
    {
        data = new XElement("Data", new XElement("ColumnName", columnName), new XElement(("Value"), value));
    }
}

该类在 WebService 中使用,当我添加对该服务的引用时,生成的对该类的引用(Reference.cs)如下:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class PairColumnValue {
}

它没有任何字段,但我无法使用它,为什么?更重要的是,我必须做什么才能获得包含我想要使用的字段的类?

PD:为了使用 WebReference,我执行以下操作:

        [Test]
    public void InsertThroughService()
    {
        ServiceReference.PairColumnValue[] toInsert = new ServiceReference.PairColumnValue[2];

        toInsert[0] = new PruebasUnitarias.ServiceReference.PairColumnValue("login", "testservice");

        toInsert[1] = new ServiceReference.PairColumnValue("password", "testservice");

        ServiceReference.PersistenceServicev2 client = new PersistenceServicev2();

        XmlSerializer serializer = new XmlSerializer(typeof(PairColumnValue));


        client.InsertData("usuario", toInsert);
    }

当然,在构造函数中它会显示“错误,该类不包含带有 2 个参数的构造函数。

帮助!当然,谢谢!”

My problem is that I have this class:

[SerializableAttribute]
[SoapInclude(typeof(PairColumnValue))]
[XmlInclude(typeof(PairColumnValue))]
public class PairColumnValue:IPairColumnValue
{
    public string ColumnName
    {
        get { return data.Element("ColumnName").Value; }
    }
    public string Value
    {
        get { return data.Element("Value").Value; }
    }


    private XElement data;

    public PairColumnValue()
    {
        data = new XElement("Data", new XElement("ColumnName", ""), new XElement(("Value"), ""));
    }

    public PairColumnValue(string columnName, string value)
    {
        data = new XElement("Data", new XElement("ColumnName", columnName), new XElement(("Value"), value));
    }
}

That class is used in a WebService, and when I add a reference to the service, the generated reference to the class (Reference.cs) is as it goes:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class PairColumnValue {
}

It doesn't have any field, and I cannot use it, why? And more important, what must I do to obtain a class with the fields that I want to use?

PD: in order to use the WebReference, I do the next:

        [Test]
    public void InsertThroughService()
    {
        ServiceReference.PairColumnValue[] toInsert = new ServiceReference.PairColumnValue[2];

        toInsert[0] = new PruebasUnitarias.ServiceReference.PairColumnValue("login", "testservice");

        toInsert[1] = new ServiceReference.PairColumnValue("password", "testservice");

        ServiceReference.PersistenceServicev2 client = new PersistenceServicev2();

        XmlSerializer serializer = new XmlSerializer(typeof(PairColumnValue));


        client.InsertData("usuario", toInsert);
    }

And, of course, at the constructors it says "Error, the class doesn't contain a constructor with 2 arguments.

Help! And thanks, for sure!

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

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

发布评论

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

评论(1

长途伴 2024-11-11 02:46:17

当您添加网络引用时,您正在传输数据(即数据的形状),而不是实现。特别是,序列化器层正在寻找它应该在另一端表示的公共读/写成员(主要是属性)。

您的属性是只读的,因此它无法重建(反序列化)您的对象;它会跳过这些属性。同样,自定义构造函数是实现,而不是数据 - 它实际上只需要一个公共无参数构造函数。

如果要共享实现,WCF 可以通过程序集共享来实现(在两端使用相同的 DTO 程序集,或相同的类型定义)。此外,默认的 WCF 序列化程序 (DataContractSerializer) 对私有访问器很满意,因此如果您添加一个私有(并告诉它通过 DataContractAttribute/DataMemberAttribute 序列化什么),它会很高兴。

When you add a web-reference you are transporting data (i.e. the data's shape), not implementation. In particular, the serializer layer is looking for public read/write members (mainly properties) that it should represent at the other end.

Your properties are read-only, so there is no way it can reconstruct (deserialize) your object; it skips those properties. Likewise, a custom constructor is implementation, not data - it only really wants a public parameterless constructor.

If you want to share implementation, WCF can do that with assembly sharing (you use the same DTO assembly at both ends, or identicial type definitions). Additionally the default WCF serializer (DataContractSerializer) is happy with private accessors, so it would be happy if you added a private set (and told it what to serialize via DataContractAttribute/DataMemberAttribute).

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