VMBase.cs 1.43 KB
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using RCS.Model.Entity;

namespace RCS.Model.Comm
{
    public abstract class VMBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public virtual void SetProp(ConcurrentBag<Base_EquipmentProp> props)
        {
            if (props == null)
            {
                return;
            }
            foreach (var item in GetType().GetProperties())
            {
                var temp = props.FirstOrDefault(t => t.Code == item.Name);
                if (temp != null)
                {
                    item.SetValue(this, temp.Value);
                    //SetPropertyValue(item, temp.Value);
                }
            }
        }

        public void SetPropertyValue(PropertyInfo propertyInfo, object value)
        {

            if (propertyInfo != null)
            {
                Type propertyType = propertyInfo.PropertyType;
                object convertedValue = Convert.ChangeType(value, propertyType);
                propertyInfo.SetValue(this, convertedValue);
            }
        }
    }
}