using System;
using System.Collections.Generic;
using System.Reflection;

namespace DeviceRepair.Models.Common
{
    public static class ObjectModelExtend
    {
        public static Dictionary<string, object> ToKeyValuePairs(this object obj)
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();

            if (obj != null)
            {
                Type objectType = obj.GetType();
                PropertyInfo[] properties = objectType.GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    // 获取所有特性
                    object[] attributes = property.GetCustomAttributes(true);

                    bool isContinue = false;
                    if (attributes != null)
                    {
                        foreach (var item in attributes)
                        {
                            if (item is SqlSugar.SugarColumn)
                            {
                                if ((item as SqlSugar.SugarColumn).IsIgnore)
                                {
                                    isContinue = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (isContinue) { continue; }

                    string propertyName = property.Name;
                    object propertyValue = property.GetValue(obj);

                    keyValuePairs.Add(propertyName, propertyValue);
                }
            }

            return keyValuePairs;
        }
    }
}
