Ejemplo n.º 1
0
 /// <summary>
 /// Direct accessor for obtaining a specific value from a configuration store.
 /// </summary>
 /// <param name="configStoreName">The named configuration store to use or null for Gentle's default store.</param>
 /// <param name="configKeyPath">The XPath to the configuration value (element or attribute).</param>
 /// <returns>The value of the given key or null if nothing was found.</returns>
 public static string GetKey(string configStoreName, string configKeyPath)
 {
     InitializeHandlers();
     lock ( cfgLock )
     {
         // set scope in which to look for key
         IList stores = handlers;
         if (configStoreName != null)
         {
             Check.Verify(namedHandlers.ContainsKey(configStoreName), Error.DeveloperError,
                          "No handler has been registered with configStoreName of {0}.", configStoreName);
             IList list = new ArrayList(1);
             list.Add(namedHandlers[configStoreName]);
             stores = list;
         }
         // obtain key and return result
         foreach (ConfigurationHandler handler in stores)
         {
             try
             {
                 XmlNode node   = handler.GetNode(configKeyPath);
                 string  result = (string)TypeConverter.Get(typeof(string), node);
                 return(result);
             }
             catch
             {
             }
         }
         return(null);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepare a <see cref="MethodInvokable"/> instance for calling the underlying method
        /// using the supplied parameters. If the call cannot be made using the given parameters,
        /// null is returned.
        /// </summary>
        /// <param name="parameters">A hashtable of parameter name/value pairs.</param>
        /// <returns>A <see cref="MethodInvokable"/> instance prepared for execution of the
        /// method call.</returns>
        public MethodInvokable PrepareInvoke(Hashtable parameters)
        {
            int normalCount  = 0;          // number of fields filled with regular parameter values
            int defaultCount = 0;          // number of fields filled using default values
            int nullCount    = 0;          // number of fields filled using null

            // process method parameters
            object[] invokeParameters = new object[parameterInfos.Length];
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                ParameterInfo pi            = parameterInfos[i];
                string        parameterName = pi.Name.ToLower();
                // ignore a leading underscore in parameter names
                if (parameterName.StartsWith("_"))
                {
                    parameterName = parameterName.Substring(1, parameterName.Length - 1);
                }
                if (parameters.ContainsKey(parameterName))
                {
                    object val = parameters[parameterName];
                    if (val != null && val.GetType() != pi.ParameterType)
                    {
                        val = TypeConverter.Get(pi.ParameterType, val);
                    }
                    invokeParameters[i] = val;
                    normalCount++;
                }
                else
                {
                    // use >= because i is 0-based whereas rP is 1-based
                    if (i >= requiredParameters)
                    {
                        // see if we have a default parameter value or if null is allowed
                        if (parameterDefaultValues[i] != null)
                        {
                            invokeParameters[i] = parameterDefaultValues[i];
                            defaultCount++;
                        }
                        else if (TypeConverter.IsNullAssignable(pi.ParameterType))
                        {
                            invokeParameters[i] = null;
                            nullCount++;
                        }
                    }
                }
            }
            // we must have a value for every method parameter
            bool isValid = parameterInfos.Length == normalCount + defaultCount + nullCount;

            // we must use all configured values in the config file
            isValid &= parameters.Count == normalCount;
            if (!isValid)
            {
                return(null);
            }
            // method can be called using supplied parameters..
            int matchIndicator = normalCount << 16 - defaultCount << 8 - nullCount;

            return(new MethodInvokable(this, matchIndicator, invokeParameters));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Use the supplied XmlNode to configure the target object. This method does not check
        /// whether the node matches the requested environment.
        /// </summary>
        /// <param name="target">The object to cofigure.</param>
        /// <param name="node">The node containing the configuration value(s).</param>
        public override void Configure(object target, XmlNode node)
        {
            // convert string to target type
            object value = TypeConverter.Get(FieldInfo.FieldType, node);

            // set value
            FieldInfo.SetValue(target, value, Reflector.InstanceCriteria, null, null);
        }