使用反射检测枚举

        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;
        }

原文地址:https://www.cnblogs.com/xixifusigao/p/1562587.html