<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-rc.2.23479.6" />

TraceUtils

static class TraceUtils
using System.Collections.Specialized; using System.Configuration; using System.Globalization; using System.IO; using System.Reflection; namespace System.Diagnostics { internal static class TraceUtils { private const string SystemDiagnostics = "System.Diagnostics."; internal static object GetRuntimeObject(string className, Type baseType, string initializeData) { object obj = null; Type type = null; if (className.Length == 0) throw new ConfigurationErrorsException(System.SR.EmptyTypeName_NotAllowed); if (className.StartsWith("System.Diagnostics.")) type = MapToBuiltInTypes(className); if (type == (Type)null) { type = Type.GetType(className); if (type == (Type)null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_find_type, className)); } if (!baseType.IsAssignableFrom(type)) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Incorrect_base_type, className, baseType.FullName)); Exception ex = null; try { if (string.IsNullOrEmpty(initializeData)) { if (IsOwnedTL(type)) throw new ConfigurationErrorsException(System.SR.TL_InitializeData_NotSpecified); ConstructorInfo constructor = type.GetConstructor(Array.Empty<Type>()); if (constructor == (ConstructorInfo)null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_get_constructor, className)); obj = constructor.Invoke(Array.Empty<object>()); } else { ConstructorInfo constructor2 = type.GetConstructor(new Type[1] { typeof(string) }); if (constructor2 != (ConstructorInfo)null) { if (IsOwnedTextWriterTL(type) && initializeData[0] != Path.DirectorySeparatorChar && initializeData[0] != Path.AltDirectorySeparatorChar && !Path.IsPathRooted(initializeData)) { string configFilePath = System.Diagnostics.DiagnosticsConfiguration.ConfigFilePath; if (!string.IsNullOrEmpty(configFilePath)) { string directoryName = Path.GetDirectoryName(configFilePath); if (directoryName != null) initializeData = Path.Combine(directoryName, initializeData); } } obj = constructor2.Invoke(new object[1] { initializeData }); } else { ConstructorInfo[] constructors = type.GetConstructors(); if (constructors == null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_get_constructor, className)); for (int i = 0; i < constructors.Length; i++) { ParameterInfo[] parameters = constructors[i].GetParameters(); if (parameters.Length == 1) { Type parameterType = parameters[0].ParameterType; try { object obj2 = ConvertToBaseTypeOrEnum(initializeData, parameterType); obj = constructors[i].Invoke(new object[1] { obj2 }); } catch (TargetInvocationException ex2) { ex = ex2.InnerException; continue; } catch (Exception ex3) { ex = ex3; continue; } break; } } } } } catch (TargetInvocationException ex4) { ex = ex4.InnerException; } if (obj == null) { if (ex != null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_create_type_instance, className), ex); throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_create_type_instance, className)); } return obj; } private static Type MapToBuiltInTypes(string className) { string text = className.Substring("System.Diagnostics.".Length); if (text != null) { switch (text.Length) { case 20: switch (text[0]) { case 'D': if (text == "DefaultTraceListener") return typeof(DefaultTraceListener); break; case 'C': if (text == "ConsoleTraceListener") return typeof(ConsoleTraceListener); break; } break; case 12: switch (text[6]) { case 'F': if (text == "SourceFilter") return typeof(SourceFilter); break; case 'S': if (text == "SourceSwitch") return typeof(SourceSwitch); break; } break; case 15: if (text == "EventTypeFilter") return typeof(EventTypeFilter); break; case 13: if (text == "BooleanSwitch") return typeof(BooleanSwitch); break; case 11: if (text == "TraceSwitch") return typeof(TraceSwitch); break; case 26: if (text == "DelimitedListTraceListener") return typeof(DelimitedListTraceListener); break; case 22: if (text == "XmlWriterTraceListener") return typeof(XmlWriterTraceListener); break; case 23: if (text == "TextWriterTraceListener") return typeof(TextWriterTraceListener); break; case 21: if (text == "EventLogTraceListener") return typeof(EventLogTraceListener); break; } } return null; } internal static bool IsOwnedTL(Type type) { if (!(typeof(EventLogTraceListener) == type)) return IsOwnedTextWriterTL(type); return true; } internal static bool IsOwnedTextWriterTL(Type type) { if (!(typeof(XmlWriterTraceListener) == type) && !(typeof(DelimitedListTraceListener) == type)) return typeof(TextWriterTraceListener) == type; return true; } private static object ConvertToBaseTypeOrEnum(string value, Type type) { if (!type.IsEnum) return Convert.ChangeType(value, type, CultureInfo.InvariantCulture); return Enum.Parse(type, value, false); } internal static void CopyStringDictionary(StringDictionary source, StringDictionary dest) { dest.Clear(); foreach (string item in source) { dest[item] = source[item]; } } } }