private static void HandleListeners(Hashtable config, XmlNode listenersNode, object context) { HandlerBase.CheckForUnrecognizedAttributes(listenersNode); foreach (XmlNode node in listenersNode.ChildNodes) { string str5; if (HandlerBase.IsIgnorableAlsoCheckForNonElement(node)) { continue; } string val = null; string str2 = null; string str3 = null; string name = node.Name; if (((str5 = name) == null) || (((str5 != "add") && (str5 != "remove")) && (str5 != "clear"))) { HandlerBase.ThrowUnrecognizedElement(node); } HandlerBase.GetAndRemoveStringAttribute(node, "name", ref val); HandlerBase.GetAndRemoveStringAttribute(node, "type", ref str2); HandlerBase.GetAndRemoveStringAttribute(node, "initializeData", ref str3); HandlerBase.CheckForUnrecognizedAttributes(node); HandlerBase.CheckForChildNodes(node); TraceListener listener = null; if (str2 != null) { Type c = Type.GetType(str2); if (c == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_find_type", new object[] { str2 })); } if (!typeof(TraceListener).IsAssignableFrom(c)) { throw new ConfigurationErrorsException(System.SR.GetString("Type_isnt_tracelistener", new object[] { str2 })); } if (str3 == null) { ConstructorInfo constructor = c.GetConstructor(new Type[0]); if (constructor == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_get_constructor", new object[] { str2 })); } listener = (TraceListener)SecurityUtils.ConstructorInfoInvoke(constructor, new object[0]); } else { ConstructorInfo ctor = c.GetConstructor(new Type[] { typeof(string) }); if (ctor == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_get_constructor", new object[] { str2 })); } listener = (TraceListener)SecurityUtils.ConstructorInfoInvoke(ctor, new object[] { str3 }); } if (val != null) { listener.Name = val; } } switch (name[0]) { case 'a': if (listener == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_create_listener", new object[] { val })); } break; case 'c': { Trace.Listeners.Clear(); continue; } case 'r': { if (listener != null) { goto Label_0258; } if (val == null) { throw new ConfigurationErrorsException(System.SR.GetString("Cannot_remove_with_null")); } Trace.Listeners.Remove(val); continue; } default: goto Label_0272; } Trace.Listeners.Add(listener); continue; Label_0258: Trace.Listeners.Remove(listener); continue; Label_0272: HandlerBase.ThrowUnrecognizedElement(node); } }
internal static object GetRuntimeObject(string className, Type baseType, string initializeData) { object obj2 = null; Type c = null; if (className.Length == 0) { throw new ConfigurationErrorsException(System.SR.GetString("EmptyTypeName_NotAllowed")); } c = Type.GetType(className); if (c == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_find_type", new object[] { className })); } if (!baseType.IsAssignableFrom(c)) { throw new ConfigurationErrorsException(System.SR.GetString("Incorrect_base_type", new object[] { className, baseType.FullName })); } Exception inner = null; try { if (string.IsNullOrEmpty(initializeData)) { if (IsOwnedTL(c)) { throw new ConfigurationErrorsException(System.SR.GetString("TL_InitializeData_NotSpecified")); } ConstructorInfo constructor = c.GetConstructor(new Type[0]); if (constructor == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_get_constructor", new object[] { className })); } obj2 = SecurityUtils.ConstructorInfoInvoke(constructor, new object[0]); } else { ConstructorInfo ctor = c.GetConstructor(new Type[] { typeof(string) }); if (ctor != null) { if ((IsOwnedTextWriterTL(c) && (initializeData[0] != Path.DirectorySeparatorChar)) && ((initializeData[0] != Path.AltDirectorySeparatorChar) && !Path.IsPathRooted(initializeData))) { string configFilePath = DiagnosticsConfiguration.ConfigFilePath; if (!string.IsNullOrEmpty(configFilePath)) { string directoryName = Path.GetDirectoryName(configFilePath); if (directoryName != null) { initializeData = Path.Combine(directoryName, initializeData); } } } obj2 = SecurityUtils.ConstructorInfoInvoke(ctor, new object[] { initializeData }); } else { ConstructorInfo[] constructors = c.GetConstructors(); if (constructors == null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_get_constructor", new object[] { 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 obj3 = ConvertToBaseTypeOrEnum(initializeData, parameterType); obj2 = SecurityUtils.ConstructorInfoInvoke(constructors[i], new object[] { obj3 }); goto Label_0223; } catch (TargetInvocationException exception2) { inner = exception2.InnerException; } catch (Exception exception3) { inner = exception3; } } } } } } catch (TargetInvocationException exception4) { inner = exception4.InnerException; } Label_0223: if (obj2 != null) { return(obj2); } if (inner != null) { throw new ConfigurationErrorsException(System.SR.GetString("Could_not_create_type_instance", new object[] { className }), inner); } throw new ConfigurationErrorsException(System.SR.GetString("Could_not_create_type_instance", new object[] { className })); }
internal static object GetRuntimeObject(string className, Type baseType, string initializeData) { Object newObject = null; #if CONFIGURATION_DEP Type objectType = null; if (className.Length == 0) { throw new ConfigurationErrorsException(SR.GetString(SR.EmptyTypeName_NotAllowed)); } objectType = Type.GetType(className); if (objectType == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_find_type, className)); } if (!baseType.IsAssignableFrom(objectType)) { throw new ConfigurationErrorsException(SR.GetString(SR.Incorrect_base_type, className, baseType.FullName)); } Exception innerException = null; try { if (String.IsNullOrEmpty(initializeData)) { if (IsOwnedTL(objectType)) { throw new ConfigurationErrorsException(SR.GetString(SR.TL_InitializeData_NotSpecified)); } // create an object with parameterless constructor ConstructorInfo ctorInfo = objectType.GetConstructor(new Type[] {}); if (ctorInfo == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className)); } newObject = SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { }); } else { // create an object with a one-string constructor // first look for a string constructor ConstructorInfo ctorInfo = objectType.GetConstructor(new Type[] { typeof(string) }); if (ctorInfo != null) { // Special case to enable specifying relative path to trace file from config for // our own TextWriterTraceListener derivatives. We will prepend it with fullpath // prefix from config file location if (IsOwnedTextWriterTL(objectType)) { if ((initializeData[0] != Path.DirectorySeparatorChar) && (initializeData[0] != Path.AltDirectorySeparatorChar) && !Path.IsPathRooted(initializeData)) { string filePath = DiagnosticsConfiguration.ConfigFilePath; if (!String.IsNullOrEmpty(filePath)) { string dirPath = Path.GetDirectoryName(filePath); if (dirPath != null) { initializeData = Path.Combine(dirPath, initializeData); } } } } newObject = SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { initializeData }); } else { // now look for another 1 param constructor. ConstructorInfo[] ctorInfos = objectType.GetConstructors(); if (ctorInfos == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className)); } for (int i = 0; i < ctorInfos.Length; i++) { ParameterInfo[] ctorparams = ctorInfos[i].GetParameters(); if (ctorparams.Length == 1) { Type paramtype = ctorparams[0].ParameterType; try { object convertedInitializeData = ConvertToBaseTypeOrEnum(initializeData, paramtype); newObject = SecurityUtils.ConstructorInfoInvoke(ctorInfos[i], new object[] { convertedInitializeData }); break; } catch (TargetInvocationException tiexc) { Debug.Assert(tiexc.InnerException != null, "ill-formed TargetInvocationException!"); innerException = tiexc.InnerException; } catch (Exception e) { innerException = e; // ignore exceptions for now. If we don't have a newObject at the end, then we'll throw. } } } } } } catch (TargetInvocationException tiexc) { Debug.Assert(tiexc.InnerException != null, "ill-formed TargetInvocationException!"); innerException = tiexc.InnerException; } if (newObject == null) { if (innerException != null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_type_instance, className), innerException); } else { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_type_instance, className)); } } #endif return(newObject); }
private static void HandleListeners(Hashtable config, XmlNode listenersNode, object context) { HandlerBase.CheckForUnrecognizedAttributes(listenersNode); foreach (XmlNode listenersChild in listenersNode.ChildNodes) { if (HandlerBase.IsIgnorableAlsoCheckForNonElement(listenersChild)) { continue; } string name = null, className = null, initializeData = null; string op = listenersChild.Name; switch (op) { case "add": case "remove": case "clear": break; default: HandlerBase.ThrowUnrecognizedElement(listenersChild); break; } HandlerBase.GetAndRemoveStringAttribute(listenersChild, "name", ref name); HandlerBase.GetAndRemoveStringAttribute(listenersChild, "type", ref className); HandlerBase.GetAndRemoveStringAttribute(listenersChild, "initializeData", ref initializeData); HandlerBase.CheckForUnrecognizedAttributes(listenersChild); HandlerBase.CheckForChildNodes(listenersChild); TraceListener newListener = null; if (className != null) { Type t = Type.GetType(className); if (t == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_find_type, className)); } if (!typeof(TraceListener).IsAssignableFrom(t)) { throw new ConfigurationErrorsException(SR.GetString(SR.Type_isnt_tracelistener, className)); } // create a listener with parameterless constructor if (initializeData == null) { ConstructorInfo ctorInfo = t.GetConstructor(new Type[] {}); if (ctorInfo == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className)); } newListener = (TraceListener)(SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { })); } // create a listener with a one-string constructor else { ConstructorInfo ctorInfo = t.GetConstructor(new Type[] { typeof(string) }); if (ctorInfo == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className)); } newListener = (TraceListener)(SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { initializeData })); } if (name != null) { newListener.Name = name; } } // we already verified above that we only have "add", "remove", or "clear", so we can // switch on the first char here for perf. switch (op[0]) { case 'a': if (newListener == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_listener, name)); } Trace.Listeners.Add(newListener); break; case 'r': if (newListener == null) { // no type specified, we'll have to delete by name // if no name is specified we can't do anything if (name == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_remove_with_null)); } Trace.Listeners.Remove(name); } else { // remove by listener Trace.Listeners.Remove(newListener); } break; case 'c': Trace.Listeners.Clear(); break; default: HandlerBase.ThrowUnrecognizedElement(listenersChild); break; } } }