在服务器端,我有以下类:
public class Customer { [Key] public int Id { get; set; } public string FirstName { get; set; } public string SecondName { get; set; } public string FullName { get { return string.Concat(FirstName, " ", SecondName); } } }
问题是每个字段都经过计算并传输到客户端(到Silvelight应用程序),例如“FullName”属性:
[DataMember()] [Editable(false)] [ReadOnly(true)] public string FullName { get { return this._fullName; } set { if ((this._fullName != value)) { this.ValidateProperty("FullName", value); this.OnFullNameChanging(value); this._fullName = value; this.RaisePropertyChanged("FullName"); this.OnFullNameChanged(); } } }
在没有手动复制属性实现的情况下,这是可能的吗?
非常感谢。
将计算出的属性作为部分类移动到不同的文件中,并利用“共享”命名转换(MyFileName.Shared.cs). 例子:
//Employee.cs public partial class Employee { [Key] public string EmployeeId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } //Employee.Shared.cs public partial class Employee { public string LastNameFirst { get { return string.Format("{0}, {1}", LastName, FirstName); } } }
共享文件中的代码将显示在客户端。