110,533
社区成员
发帖
与我相关
我的任务
分享
namespace Models
{
public class Person
{
public string UserName { get; set; }
}
}
namespace Contracts
{
[ServiceContract]
public interface IService
{
[OperationContract]
string DoSomething(Person p);
}
}
namespace Services
{
public class MyService : IService
{
public string DoSomething(Person p)
{
return p.UserName;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceClient client = new ServiceClient();
Person P = new Person() { UserName = "张三"};
MessageBox.Show(client.DoSomething(P));
}
namespace Models
{
public class Person
{
public string UserName { get; set; }
public ABC abc { get; set; }
}
}
public class ABC
{
public string a{ get; set; }
public int b{ get; set; };
}
//其他的不写了,就上面的你可以贴进去试试。
原因很简单,Person类中原先只有一个string的属性,他是属于“有效的数据协定类”,但现在给我加进去一个ABC类,这是个自定义类型,并且我没有将DataContract应用于该类,这下明白DataContract的作用了吧?看红字就知道了。Person P = new Person() { UserName = "张三"};
这里的Person类型的根本就是 ServiceClient 所在的同一个命名空间中的那个 Person 类,而根本不是 Models 命名空间中的 Person 类!