2008-12-03 53 views
5

我想通过Web服务发送一个匿名对象。无论如何,我可以做到这一点,而无需手动创建一个类并将其转换为该类?目前它抛出一个异常说匿名对象不能序列化。如何序列化通过SOAP 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; 
} 

在此先感谢。

编辑:这里是我的意思是通过手动创建一个类来返回,并与匿名对象填充一个例子...

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; 
} 

回答

7

匿名类型是为了用于简单的预测非常松散耦合的数据,仅在方法中使用。如果Web方法返回某种类型的数据是有意义的,那么它确实应该体面封装。换句话说,即使你可以通过找到一种方法来从Web方法返回一个匿名类型的实例,我强烈建议你不要这样做。

我个人不会使用公共字段创建类 - 使用自动实现的属性来代替,所以如果需要,可以安全地添加更多行为。

请注意,创建“正确”类型后,查询表达式只需要非常轻微地更改 - 只需在new{之间添加类型名称即可使用对象初始值设定项。

+0

只是好奇,为什么你喜欢自动在公共领域实现的属性? – Kevin 2008-12-03 15:19:17

1

这是我使用的代码。

[WebMethod(EnableSession = true)] 
public PatientsResult GetPatientList(bool returnInactivePatients) { 
    if (!IsLoggedIn()) { 
     return new PatientsResult() { 
      Success = false, 
      LoggedIn = false, 
      Message = "Not logged in" 
     }; 
    } 
    Func<IEnumerable<PatientResult>, IEnumerable<PatientResult>> filterActive = 
     patientList => returnInactivePatients ? patientList : patientList.Where(p => p.Status == "Active"); 
    User u = (User)Session["user"]; 
    return new PatientsResult() { 
     Success = true, 
     LoggedIn = true, 
     Message = "", 
     Patients = filterActive((from p in u.Practice.Patients 
         select new PatientResult() { 
          PhysicianID = p.PhysicianID, 
          Status = p.Active ? "Active" : "Inactive", 
          PatientIdentifier = p.PatientIdentifier, 
          PatientID = p.PatientID, 
          LastVisit = p.Visits.Count > 0 ? p.Visits.Max(v => v.VisitDate).ToShortDateString() : "", 
          Physician = (p.Physician == null ? "" : p.Physician.FirstName + " " + p.Physician == null ? "" : p.Physician.LastName).Trim(), 
         })).ToList<PatientResult>() 
    }; 
} 
public class Result { 
    public bool Success { get; set; } 
    public bool LoggedIn { get; set; } 
    public string Message { get; set; } 
} 
public class PatientsResult : Result { 
    public List<PatientResult> Patients { get; set; } 
} 
public class PatientResult { 
    public int PatientID { get; set; } 
    public string Status { get; set; } 
    public string PatientIdentifier { get; set; } 
    public string Physician { get; set; } 
    public int? PhysicianID {get;set;} 
    public string LastVisit { get; set; } 
} 

}