using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace DrawGraph
{
    public class BoardFormUtil
    {
        public static AreaManageBase AreaManageFactory(string instanceName, object parameter, ZedGraphControl parameter1, TemplateManage parameter2, string parameter3)
        {
            AreaManageBase reValue = null;
            try
            {
                Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集 
                //object obj = assembly.CreateInstance("ReflectionTest.Test"); //类的完全限定名(即包括命名空间)
                object[] parameters = new object[4];
                parameters[0] = parameter;
                parameters[1] = parameter1;
                parameters[2] = parameter2;
                parameters[3] = parameter3;
                reValue = (AreaManageBase)assembly.CreateInstance("DrawGraph." + instanceName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);// 创建类的实例
                if(reValue == null)
                    reValue = (AreaManageBase)assembly.CreateInstance("DrawGraph.document." + instanceName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);
                if (reValue != null) reValue.ClassName = instanceName;
            }
            catch(Exception  )
            {
            }
            return reValue;
        }
        /// 
        /// 
        /// 
        /// 
        /// DLL文件名,必须在当前运行项目的目录里
        /// 命名空间名
        /// 类名
        /// 
        public static T GetObjectFactory(string dllName,string nameSpace,string instanceName){
            T reValue;
            try
            {
                Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + dllName);//你的loadfile
                //Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集 
                //object obj = assembly.CreateInstance("ReflectionTest.Test"); //类的完全限定名(即包括命名空间)
                reValue = (T)assembly.CreateInstance(nameSpace + instanceName, true, System.Reflection.BindingFlags.Default, null, null, null, null);// 创建类的实例
            }
            catch
            {
                reValue = default;
            }
            return reValue;
        }
        /// 
        /// 获取对象的所有属性列表
        /// 
        /// 
        /// 
        public static List GetPropertyList(Object obj, TemplateManage template = null)
        {
            List propertyList = new List();
            if (obj != null)
            {
                Type type = obj.GetType();
                PropertyInfo[] infos = type.GetProperties();
                foreach (PropertyInfo info in infos)
                {
                    try {
                        Attribute[] attribs = Attribute.GetCustomAttributes(info);
                        Attribute attrib = attribs.FirstOrDefault(s => s is NoCreatControlAttributs);
                        //如果增加了NoCreatControlAttributs这个属性的属性则不生成列表
                        if (attrib != null) continue;
                        PropertyObject proObj = new PropertyObject();
                        proObj.Key = info.Name;
                        Attribute attDesc = attribs.FirstOrDefault(s => s is ClassAttributs);
                        if (attDesc != null)
                        {
                            ClassAttributs cattr = attDesc as ClassAttributs;
                            if (cattr != null)
                            {
                                proObj.Description = cattr.Description;
                            }
                        }
                        //如果不是值类型,且没标明ClassAttributs属性标签的引用类型,就继续往下循环
                        if (info.PropertyType != typeof(Color) && info.PropertyType != typeof(int) && info.PropertyType != typeof(string) && info.PropertyType != typeof(DateTime) && info.PropertyType != typeof(Decimal) && info.PropertyType != typeof(bool) && info.PropertyType != typeof(Nullable) && info.PropertyType != typeof(Nullable) && info.PropertyType != typeof(Nullable) && info.PropertyType != typeof(Nullable))
                        {
                            proObj.SubPropertyList = GetPropertyList(info.GetValue(obj, null), template);
                        }
                        else
                        {
                            object val = info.GetValue(obj, BindingFlags.GetProperty, null, null, null);
                            proObj.Value = (val != null) ? val.ToString() : "";
                            proObj.Text = proObj.Value;
                        }
                        propertyList.Add(proObj);
                    } catch (Exception exp) {
                        MessageBox.Show(exp.Message.ToString());
                    }                    
                }
            }
            else
            {
                //Console.WriteLine(obj.ToString());
            }
            return propertyList;
        }
    }
}