使用反射,查找WCF异常类型

时间:2021-09-07 07:48:56

//使用System.Reflection,查找System.ServiceModel的异常类型
        public void ConsoleException()
        {
            try
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                foreach (AssemblyName item in asm.GetReferencedAssemblies())
                {
                    if (item.Name != "System.ServiceModel") continue;
                    foreach (Type t in Assembly.Load(item).GetTypes())
                    {
                        if (t.Name.EndsWith("Exception"))
                        {
                            Console.WriteLine(t.Name);
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine( ex.Message);
            }
        }