You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.9 KiB
80 lines
1.9 KiB
3 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 利用反射来判断对象是否包含某个属性
|
||
|
/// </summary>
|
||
|
/// <param name="instance">object</param>
|
||
|
/// <param name="propertyName">需要判断的属性</param>
|
||
|
/// <returns>是否包含</returns>
|
||
|
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<T>()
|
||
|
{
|
||
|
string json = JsonConvert.SerializeObject(this);
|
||
|
return JsonConvert.DeserializeObject<T>(json);
|
||
|
}
|
||
|
}
|
||
|
}
|