zoukankan      html  css  js  c++  java
  • 使用反射检测枚举

            public void CheckAllEnums(string dllPath)
            {
                Assembly assembly = Assembly.LoadFrom(dllPath);
                Type[] types = assembly.GetTypes();
                CreateFile();
                foreach (Type t in types)
                {
                    PropertyInfo[] infos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    object objectHandle = null;
                    try
                    {
                        if (t.GetConstructors().Length > 0 && t.Name.Length > 2 && !t.Namespace.Contains("Design"))
                        {
                            objectHandle = Activator.CreateInstance(t);
                        }

                        if (objectHandle != null)
                        {
                            foreach (PropertyInfo item in infos)
                            {
                                Console.WriteLine(item.Name);
                                bool flag = false;
                                if (item.PropertyType.IsEnum)
                                {
                                    bool isb = IsBrowsable(item);
                                    if (item.CanWrite)
                                    {
                                        try
                                        {
                                            t.InvokeMember(item.Name, BindingFlags.SetProperty, null, objectHandle,
                                                new object[] { Enum.ToObject(item.PropertyType, -200) });
                                            flag = IsSpecifiedAttribute(typeof(FlagsAttribute), item);
                                            if (flag)
                                            {
                                                continue;
                                            }
                                            WriteToFile("NoException : " + t.Name + "." + item.Name);
                                        }
                                        catch (Exception exp)
                                        {
                                            if (exp.InnerException != null)
                                            {
                                                Type eType = exp.InnerException.GetType();
                                                if (!eType.Equals(typeof(InvalidEnumArgumentException)))
                                                {
                                                    WriteToFile(t.Name + "." + item.Name);
                                                }
                                            }
                                            else
                                                WriteToFile(t.Name + "." + item.Name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch
                    { }
                }
            }

            private bool IsSpecifiedAttribute(Type attributeType, PropertyInfo item)
            {
                object[] attributes = item.PropertyType.GetCustomAttributes(true);
                foreach (object attr in attributes)
                {
                    Type fa = attr.GetType();
                    if (fa == attributeType)
                    {
                        return true;
                    }
                }
                return false;
            }

            private void WriteToFile(string line)
            {
                using (StreamWriter writer = File.AppendText("log.txt"))
                {
                    writer.WriteLine(line);
                }
            }

            private void CreateFile()
            {
                if (File.Exists("log.txt"))
                {
                    File.Delete("log.txt");
                }

                using (FileStream file = File.Create("log.txt"))
                {

                }
            }

            private bool IsBrowsable(PropertyInfo item)
            {
                object[] attributes = item.PropertyType.GetCustomAttributes(true);
                foreach (object attr in attributes)
                {
                    Type fa = attr.GetType();
                    if (fa == typeof(EditorBrowsableAttribute))
                    {
                        return false;
                    }
                }
                return true;
            }

  • 相关阅读:
    mac升级后,遇到openssl相关问题
    mysql清空所有表
    composer 管理js css等依赖文件【fxp/composer-asset-plugin】
    php安装pcntl
    git命令
    docker
    OAuth 2.0
    mysql杯观锁与乐观锁
    mysql添加用户,授权,刷新权限
    Mac下安装SecureCRT并激活
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1562587.html
Copyright © 2011-2022 走看看