C# web service 返回 自定义对象
杨柳晓枫 2013-09-04 11:15:45 User.cs 代码如下
***********************************************
namespace DataModel
{
[Serializable()]
public class User
{
public User()
{
}
public User(int id,string name)
{
id = id;
name = name;
}
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
#region DB IO
public User RetrunUserSingle(SqlConnection connection)
{
using (SqlCommand command=connection.CreateCommand())
{
command.CommandText = "select * from [user] where ID=1";
SqlDataReader dr = command.ExecuteReader();
Reader(dr);
User user =new User(id,name);
return user;
}
}
public void Reader(SqlDataReader dr)
{
if (dr.Read())
{
name = dr.GetString(dr.GetOrdinal("Name"));
id = dr.GetInt32(dr.GetOrdinal("ID"));
}
}
#endregion
}
}
***********************************************
webservice 页面 代码如下
***********************************************
[WebMethod]
public User GetOneUser(SqlConnection connection)
{
User user = new User();
return user.RetrunUserSingle(connection);
}
***********************************************
总是报错,错误信息:
Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.
希望赐教,越详细越好!