zoukankan      html  css  js  c++  java
  • 关于自定义实体类在webservice调用时property丢失的问题

    三个project,一个client,一个webservice,一个webservicecomponent。
    client代码:

    localhost.ToolType[] types;
    localhost.Service1 s 
    = new localhost.Service1();
    types 
    = s.GetToolType();
    comboBox1.DisplayMember 
    = "Name";
    comboBox1.ValueMember 
    = "Code";
    comboBox1.DataSource 
    = types;

    webservice的代码:
    [WebMethod]
    public ToolType[] GetToolType()
    {
        ToolType[] types;

        types 
    = new ToolType[3];
        types[
    0= new ToolType();
        types[
    0].Code = 1;
        types[
    0].Name = "COM+";
        types[
    0].ImplClass = "test";

        types[
    1= new ToolType();
        types[
    1].Code = 2;
        types[
    1].Name = ".NET";
        types[
    1].ImplClass = "test2";

        types[
    2= new ToolType();
        types[
    2].Code = 3;
        types[
    2].Name = "ShellExecute";
        types[
    2].ImplClass = "test3";

        
    return types;
    }

    后台component的代码:
    public class ToolType
    {
        
    private int code;
        
    private string name,implclass;

        
    /// <summary>编号</summary>
        public int Code{get{return code;}set{code = value;}}

        
    /// <summary>名称</summary>
            public string Name{get{return name;}set{name = value;}}
        
    /// <summary>可以对该类型进行解析的类</summary>
        public string ImplClass{get{return implclass;}set{implclass = value;}}
    }


    注意!上面的service中的代码,其实不应该这么写。而再写一个ToolTypeManager类,提供一个Read的方法。我这里为了简单,就把这段代码直接放到service里面了。

    好了!到说问题的时候了!

    当我在client调用service的时候,返回的ToolType中(可以看reference.cs文件),所有的property都变成了普通的field,亦即:get/set都没有了。这样,我用这个数组来帮定一个control的时候,出错了!显示的是namespace.classname,而不是具体的值。这个原因,就是因为.net绑定的时候,是按照property来的,而不是field来的(可以参见:C#高级编程,就是那本巨厚的书)

    这个问题,我不知道咋解决。后来,把代码修改了,返回dataset,而不是我自己的实体类,就搞定了。
  • 相关阅读:
    ASP.NET实现年月日三级联动(局部刷新)
    通过ip地址获取当前地理位置
    Microsoft.Practices.Unity 给不同的对象注入不同的Logger
    sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。
    bindingredirect 没有效果
    Expression构建DataTable to Entity 映射委托
    windows 2003 远程桌面 连接输入账号密码后,只能看见蓝色屏幕和鼠标
    vmware workstation 10.0.1 install mac os mavericks
    教师节我的专属祝福方式!
    信盈达学员学习心得
  • 原文地址:https://www.cnblogs.com/juqiang/p/28668.html
Copyright © 2011-2022 走看看