using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FLY.OBJComponents.Common
{
    public static class PropertiesManager_JSON
    {
        /// <summary>
        /// 设定参数（属性）的值，触发属性值变化“通知”,writeVal
        /// </summary>
        /// <param name="propertyName">属性名</param>
        /// <param name="val">属性值</param>
        public static void SetValue(object obj, string propertyName, object v)
        {
            PropertyInfo property = obj.GetType().GetProperties().First(p => { return p.Name == propertyName; });
            if (property != null)
            {
                if (v.GetType() == property.PropertyType)
                {
                    property.SetValue(obj, v, null);
                }
                else
                {
                    string json = JsonConvert.SerializeObject(v);
                    property.SetValue(obj, JsonConvert.DeserializeObject(json, property.PropertyType), null);
                }
            }
        }

    }
}
