using Newtonsoft.Json; using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; namespace EC.Entity.PublicModel { public class BaseEntity : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public bool SetValue(string propertyName, string value) { if (!ContainProperty(propertyName)) { return false; } try { Type type = GetType(); object objVal = Convert.ChangeType(value, type.GetProperty(propertyName).PropertyType); type.GetProperty(propertyName).SetValue(this, objVal, null); OnPropertyChanged(propertyName); return true; } catch { return false; } } public object GetValue(string propertyName) { if (!ContainProperty(propertyName)) { return null; } try { Type type = GetType(); object objVal = type.GetProperty(propertyName).GetValue(this, null); return objVal; } catch { return null; } } /// /// 利用反射来判断对象是否包含某个属性 /// /// object /// 需要判断的属性 /// 是否包含 public bool ContainProperty(string propertyName) { if (this != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = GetType().GetProperty(propertyName); return _findedPropertyInfo != null; } return false; } [NotMapped] public string FaultUri { get; set; } = "-1"; public virtual bool IsUriGain(string uri) { return !(string.IsNullOrEmpty(uri) || uri.Equals(FaultUri, StringComparison.Ordinal)); } public virtual T DeepCopy() { string json = JsonConvert.SerializeObject(this); return JsonConvert.DeserializeObject(json); } } }