Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

序列化的问题 #33

Open
snikeguo opened this issue Nov 27, 2020 · 1 comment
Open

序列化的问题 #33

snikeguo opened this issue Nov 27, 2020 · 1 comment

Comments

@snikeguo
Copy link

snikeguo commented Nov 27, 2020

当客户端和服务端数据交互的类 的特性不一致 就会RPC调用失败:
这句话可能说的不太明白,请看代码:
服务端:

public class student
{
  public int a{get;set;}
  public int b{get;set;}
}
public interface IStudentService
{
     AddStudent(student s);
}

客户端

[JsonObject] //添加特性
public class student
{
[JsonProperty]
  public int a{get;set;}
[JsonProperty]
  public int b{get;set;}
}
.....

student stu= DeserializeObject(file.readtext());

Client.AddStudent(stu);//调用失败!!!!!

在客户端,上述代码中的student类即作为json的存储类,又作为和server交互的数据类,上述写法会调用失败,除非我把student的特性去掉。现阶段我的解决方案是这么写的:

[JsonObject] //添加特性
public class  JsonStudent
{
[JsonProperty]
  public int a{get;set;}
[JsonProperty]
  public int b{get;set;}
}
public class student
{
  public int a{get;set;}
  public int b{get;set;}
}
JsonStudent jsonstu= DeserializeObject(file.readtext());
student rpc_stu=new stduent();
rpc_stu.a=jsonstu.a;
rpc_stu.b=jsonstu.b;

Client.AddStudent(rpc_stu);//调用成功!!!

可见,客户端的类和服务器的类必须一致才可以,在这个例子中束缚了写法,必须定义两个类,我认为客户端和服务端的数据交互 传递的有效数据是 各个类中的具体字段、属性等等, 特性这个应该被忽略掉,这样我最开始的第一种写法就可以了,就不用重新定义类了

@beetlex-io
Copy link
Owner

由于方法是可重载多版本的,所以必须符类型名称的参数方法才能调用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants