/// <summary> /// Sets a paramater on an object. /// </summary> /// <remarks> /// The parameter name must correspond to a writable property /// on the object. The value of the parameter is a string, /// therefore this function will attempt to set a string /// property first. If unable to set a string property it /// will inspect the property and its argument type. It will /// attempt to call a static method called 'Parse' on the /// type of the property. This method will take a single /// string argument and return a value that can be used to /// set the property. /// </remarks> /// <param name="element">The parameter element.</param> /// <param name="target">The object to set the parameter on.</param> protected void SetParameter(XmlElement element, object target) { // Get the property name string name = element.GetAttribute(NAME_ATTR); // If the name attribute does not exist then use the name of the element if (element.LocalName != PARAM_TAG || name == null || name.Length == 0) { name = element.LocalName; } // Look for the property on the target object Type targetType = target.GetType(); Type propertyType = null; PropertyInfo propInfo = null; MethodInfo methInfo = null; // Try to find a writable property propInfo = targetType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (propInfo != null && propInfo.CanWrite) { // found a property propertyType = propInfo.PropertyType; } else { propInfo = null; // look for a method with the signature Add<property>(type) methInfo = targetType.GetMethod("Add" + name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (methInfo != null && methInfo.IsPublic && !methInfo.IsStatic) { System.Reflection.ParameterInfo[] methParams = methInfo.GetParameters(); if (methParams.Length == 1) { propertyType = methParams[0].ParameterType; } else { methInfo = null; } } else { methInfo = null; } } if (propertyType == null) { LogLog.Error("DOMConfigurator: Cannot find Property [" + name + "] to set object on [" + target.ToString() + "]"); } else { if (element.GetAttributeNode(VALUE_ATTR) != null) { string propertyValue = element.GetAttribute(VALUE_ATTR); // Fixup embedded non-printable chars propertyValue = OptionConverter.ConvertSpecialChars(propertyValue); #if !NETCF try { // Expand environment variables in the string. propertyValue = OptionConverter.SubstVars(propertyValue, Environment.GetEnvironmentVariables()); } catch (System.Security.SecurityException) { // This security exception will occur if the caller does not have // unrestricted environment permission. If this occurs the expansion // will be skipped with the following warning message. LogLog.Debug("DOMConfigurator: Security exception while trying to expand environment variables. Error Ignored. No Expansion."); } #endif // Now try to convert the string value to an acceptable type // to pass to this property. object convertedValue = ConvertStringTo(propertyType, propertyValue); if (convertedValue != null) { if (propInfo != null) { // Got a converted result LogLog.Debug("DOMConfigurator: Setting Property [" + propInfo.Name + "] to " + convertedValue.GetType().Name + " value [" + convertedValue.ToString() + "]"); // Pass to the property propInfo.SetValue(target, convertedValue, BindingFlags.SetProperty, null, null, CultureInfo.InvariantCulture); } else if (methInfo != null) { // Got a converted result LogLog.Debug("DOMConfigurator: Setting Collection Property [" + methInfo.Name + "] to " + convertedValue.GetType().Name + " value [" + convertedValue.ToString() + "]"); // Pass to the property methInfo.Invoke(target, BindingFlags.InvokeMethod, null, new object[] { convertedValue }, CultureInfo.InvariantCulture); } } else { LogLog.Warn("DOMConfigurator: Unable to set property [" + name + "] on object [" + target + "] using value [" + propertyValue + "] (with acceptable conversion types)"); } } else { // No value specified Type defaultObjectType = null; if (propertyType.IsClass && !propertyType.IsAbstract) { defaultObjectType = propertyType; } object createdObject = CreateObjectFromXml(element, defaultObjectType, propertyType); if (createdObject == null) { LogLog.Error("DOMConfigurator: Failed to create object to set param: " + name); } else { if (propInfo != null) { // Got a converted result LogLog.Debug("DOMConfigurator: Setting Property [" + propInfo.Name + "] to object [" + createdObject + "]"); // Pass to the property propInfo.SetValue(target, createdObject, BindingFlags.SetProperty, null, null, CultureInfo.InvariantCulture); } else if (methInfo != null) { // Got a converted result LogLog.Debug("DOMConfigurator: Setting Collection Property [" + methInfo.Name + "] to object [" + createdObject + "]"); // Pass to the property methInfo.Invoke(target, BindingFlags.InvokeMethod, null, new object[] { createdObject }, CultureInfo.InvariantCulture); } } } } }