关于自定义实体类在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;
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;
}
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;}}
}
{
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,而不是我自己的实体类,就搞定了。