设置属性时获取StackOverflowException

public List ListarEmpleados() { List returnList = new List(); var lista = from u in DB.tabEmpleado select new { u.idEmpleado, u.idUsuario, u.Nombre, u.Apellidos, u.Telefono1 }; foreach (var e in lista) { Empleado empleado = new Empleado(); empleado.idEmpleado = e.idEmpleado; empleado.idUsuario = e.idUsuario; empleado.nombre = e.Nombre; empleado.apellidos = e.Apellidos; empleado.telefono1 = e.Telefono1; returnList.Add(empleado); } return returnList; } 

这是一个WCF服务,当调用它时,它会在类定义中返回StackOverflow错误,完全在idEmpleado的Set属性中。

类定义在这里。

 [DataContract] public class Empleado { private int _idEmpleado; [DataMember(IsRequired = false)] public int idEmpleado { get { return _idEmpleado; } set { idEmpleado = value; } ERROR } private int _idUsuario; [DataMember(IsRequired = false)] public int idUsuario { get { return _idUsuario; } set { idUsuario = value; } } private string _nombre; [DataMember(IsRequired = false)] public string nombre { get { return _nombre; } set { nombre = value; } } private string _apellidos; [DataMember(IsRequired = false)] public string apellidos { get { return _apellidos; } set { apellidos = value; } } private string _telefono1; [DataMember(IsRequired = false)] public string telefono1 { get { return _telefono1; } set { telefono1 = value; } } } 

}

有谁知道错误在哪里?

提前致谢。

您通过再次调用属性设置器来设置属性的值,而不是直接设置其支持字段。 这导致无限递归和堆栈溢出。

 public int idEmpleado { get { return _idEmpleado; } set { idEmpleado = value; } // SHOULD BE _idEmpleado = value }