PhoneToolMX/PhoneToolMX.Models/Models/IModel.cs

22 lines
704 B
C#

using System.Reflection;
namespace PhoneToolMX.Models
{
public interface IModel
{
public int? Id { get; set; }
public void Commit(IModel obj)
{
if (GetType() != obj.GetType()) throw new ArgumentException("Input object must be the same model");
foreach (var prop in GetType().GetProperties().Where(p => p.CanWrite)) {
if (prop.GetValue(obj, null) is {} propVal) {
prop.SetValue(this, propVal, null);
} else if (prop.GetCustomAttributes(typeof(AllowNullAttribute)).FirstOrDefault() != null) {
prop.SetValue(this, null, null);
}
}
}
}
}