Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何序列化匿名对象以通过SOAP web服务发送?_C#_Asp.net_Web Services_Serialization_Soap - Fatal编程技术网

C# 如何序列化匿名对象以通过SOAP web服务发送?

C# 如何序列化匿名对象以通过SOAP web服务发送?,c#,asp.net,web-services,serialization,soap,C#,Asp.net,Web Services,Serialization,Soap,我正在尝试通过web服务发送匿名对象。我是否可以在不手动创建类并将其强制转换到该类的情况下执行此操作?目前它正在抛出一个异常,表示匿名对象无法序列化 // Some code has been removed here to simplify the example. [WebMethod(EnableSession = true)] public Response GetPatientList() { var patientList = from patient in ((User)S

我正在尝试通过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;
}

匿名类型用于非常松散耦合的数据的简单投影,仅在方法中使用。如果web方法返回某种类型的数据是有意义的,那么它确实应该被适当地封装。换句话说,即使您可以找到从web方法返回匿名类型实例的方法,我强烈建议您不要这样做

我个人也不会创建带有公共字段的类,而是使用自动实现的属性,这样以后如果需要,您可以安全地添加更多行为


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

以下是我最后使用的代码

[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; }
}
[WebMethod(EnableSession=true)]
公共患者结果GetPatientList(bool RETURN非活动患者){
如果(!IsLoggedIn()){
返回新的PatientsResult(){
成功=错误,
LoggedIn=false,
Message=“未登录”
};
}
Func filterActive=
patientList=>returnInactivePatients?patientList:patientList.Where(p=>p.Status==“Active”);
用户u=(用户)会话[“用户”];
返回新的PatientsResult(){
成功=正确,
LoggedIn=true,
Message=“”,
患者=过滤器活性((来自美国实践中的p)患者
选择新PatientResult(){
PhysicianID=p.PhysicianID,
状态=p.活动?“活动”:“不活动”,
PatientIdentifier=p.PatientIdentifier,
PatientID=p.PatientID,
LastVisit=p.visions.Count>0?p.visions.Max(v=>v.VisitDate).ToSortDateString():“”,
内科医生=(p.medical==null?“:p.medical.FirstName+”“+p.medical==null?”:p.medical.LastName).Trim(),
}))托利斯先生()
};
}
公开课成绩{
公共bool成功{get;set;}
公共bool LoggedIn{get;set;}
公共字符串消息{get;set;}
}
公共类PatientsResult:结果{
公共列表患者{get;set;}
}
公共类PatientResult{
public int PatientID{get;set;}
公共字符串状态{get;set;}
公共字符串PatientIdentifier{get;set;}
公共字符串{get;set;}
公共int?PhysicianID{get;set;}
公共字符串LastVisit{get;set;}
}

}

只是好奇,为什么您更喜欢自动实现的属性而不是公共字段?如果您返回的是json之类的序列化消息,这难道不意味着您同意它是松散耦合的吗?这似乎仍然是一个有效的用例。@pc1oad1etter:“松散耦合”一词在这里有不同的用法。我是从数据本身的角度讲的——数据项之间只是有一些偶然的联系。例如,这与松散耦合到web服务不同。