我有一个 Employee
类,每个员工都有一个申请休假的列表。是否可以将列表 AppliedLeave
作为 WCF 中的 [DataMember]
?
[DataContract]
public class Employee
{
[DataMember]
public string UserID { get; set; }
[DataMember]
public int EmployeeNumber { get; set; }
[ForeignKey("EmployeeUserID")]
[DataMember]
public List<Leave> AppliedLeave
{
get { return _appliedLeaves; }
set { _appliedLeaves = value; }
}
private List<Leave> _appliedLeaves = new List<Leave>();
...
}
还有其他方法吗?
感谢您对此事的考虑
我扩展我的问题
这是我的休假类(class):
[DataContract]
public class Leave
{
[Key()]
[DataMember]
public Guid LeaveId { get; set; }
[DataMember]
public string LeaveType { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public string EmployeeUserID { get; set; }
}
这显示了 ServiceContract ---->
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
Employee GetEmployeeByUserId(string userId);
[OperationContract]
void AssignSupervisor(string userId, string supervisorUserId);
[OperationContract]
void DeleteEmployeeByUserId(string userId);
....
}
在客户端应用中,
EmployeeServiceClient employeeService = new EmployeeServiceClient();
Employee employee = employeeService.GetEmployeeByUserId(id);
但是当 Employee 从服务中收集时,它的叶子显示为 Null,
有人可以帮助我吗?我在这里做错了什么?
最佳答案
是的,可以从 WCF 服务操作返回泛型。
但默认情况下,它们在客户端被转换为数组。这可以在代理生成时自定义。
WCF: Serialization and Generics
此外,您还必须使用 KnownTypeAttribute 使用泛型可以解析为的所有类型来装饰服务。
关于c# - 使用 WCF 服务返回 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646978/