Sha*_*awn 5 c# asp.net serialization soap web-services
我试图通过Web服务发送匿名对象.反正我是否可以在不手动创建类并将其强制转换为该类的情况下执行此操作?目前它抛出一个异常,说匿名对象无法序列化.
// Some code has been removed here to simplify the example.
[WebMethod(EnableSession = true)]
public Response GetPatientList() {
var patientList = from patient in ((User)Session["user"]).Practice.Patients
select new {
patientID = patient.PatientID,
status = patient.Active ? "Active" : "Inactive",
patientIdentifier = patient.PatientIdentifier,
physician = (patient.Physician.FirstName + " " + patient.Physician.LastName).Trim(),
lastModified = patient.Visits.Max(v => v.VisitDate)
};
return patientList;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑:这是一个我的意思的例子,手动创建一个类来返回并用匿名对象填充它...
public class Result {
public bool success;
public bool loggedIn;
public string message;
}
public class PracticeInfoResult : Result {
public string practiceName;
public string address;
public string city;
public string state;
public string zipCode;
public string phone;
public string fax;
}
Run Code Online (Sandbox Code Playgroud)
匿名类型旨在用于非常松散耦合的数据的简单投影,仅在方法中使用.如果Web方法返回类型的数据是有意义的,那么它实际上应该被适当地封装.换句话说,即使您可以找到一种从Web方法返回匿名类型实例的方法,我强烈建议您不要这样做.
我个人不会使用公共字段创建类 - 使用自动实现的属性,因此如果需要,您可以安全地添加更多行为.
请注意,在创建"正确"类型之后,您的查询表达式只需要非常非常轻微地更改 - 只需在类之间添加类型名称new
并{
使用对象初始值设定项.
归档时间: |
|
查看次数: |
3751 次 |
最近记录: |