Exemple #1
0
        /// <summary>
        /// Converts an object to a parameter set.
        /// </summary>
        /// <param name="data">The object to convert.</param>
        /// <param name="addWithIndex">if set to <c>true</c> values are also added by index, e.g. new { Test = 10 } will be added both as "Test" and "0".</param>
        /// <returns></returns>
        public static ParameterSet ParamsToParameterSet(object data, bool addWithIndex = false)
        {
            if (data is string)
            {
                throw new LocalizedArgumentException("ObjectHelper.ParamsToParameterSet.DataCannotBeString");
            }

            object[] array = data as object[];
            if (array != null)
            {
                var numberedValues = new Dictionary <string, object>();
                if (data != null)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        numberedValues.Add("" + i, array[i]);
                    }
                }

                return(new DicitionaryParameterSet(numberedValues));
            }
            else
            {
                var paramSet = data as ParameterSet;
                if (paramSet != null)
                {
                    return(paramSet);
                }
                else
                {
                    var dictionary = data as IDictionary <string, object>;
                    if (dictionary != null)
                    {
                        return(new DicitionaryParameterSet(dictionary));
                    }
                    else
                    {
                        var parameters = new DicitionaryParameterSet();
                        if (data != null)
                        {
                            int i = 0;

                            var attr = BindingFlags.Public | BindingFlags.Instance;
                            foreach (var property in data.GetType().GetProperties(attr))
                            {
                                if (property.CanRead)
                                {
                                    var val = property.GetValue(data, null);
                                    parameters.SetObject(property.Name, val);
                                    if (addWithIndex)
                                    {
                                        parameters.SetObject("" + i, val);
                                    }
                                    ++i;
                                }
                            }
                        }
                        return(parameters);
                    }
                }
            }
        }
        //If another namespace is needed for the pattern lookup it can be written as Namespace__Key, i.e. namespace and key seperated by two underscores '_'
        public ParameterValue GetValue(EvaluationContext context)
        {            
            var values = context.Parameters;


            string actualPatternKey;
            if (PatternKey.StartsWith("@"))
            {
                var parts = PatternKey.Substring(1).Split('+');
                actualPatternKey = (string)values.GetObject(parts[0]);
                if (parts.Length > 1)
                {
                    actualPatternKey += parts[1];
                }                
            }
            else
            {
                actualPatternKey = PatternKey;
            }
            
            string ns = context.Namespace;

            int ix = actualPatternKey.IndexOf(NamespaceQualifier);
            if (ix != -1)
            {
                ns = actualPatternKey.Substring(0, ix);
                actualPatternKey = actualPatternKey.Substring(ix + NamespaceQualifier.Length);
            }            

            if (actualPatternKey != null)
            {
                int i = 0;
                var callValues = new DicitionaryParameterSet();                
                foreach (var p in Parameters)
                {                    
                    
                    if ((p.Key.StartsWith("\"") || p.Key.StartsWith("'")) && (p.Key.EndsWith("\"") || p.Key.EndsWith("'")))
                    {
                        callValues[""+i] = ParameterValue.Wrap(p.Key.Substring(1, p.Key.Length - 2));                        
                    }
                    else
                    {
                        ParameterValue v = null;
                        if (p.Value != null)
                        {
                            v = values[p.Key].Clone();
                            v.DefaultFormat = p.Value;
                        }
                        else
                        {
                            v = values[p.Key];
                        }
                        
                        callValues[p.Key] = v;
                        callValues["" + i] = v;
                    }
                    ++i;
                }                

                return new UnencodedParameterValue(Manager.Get(actualPatternKey, callValues,
                    ns: ns, language: context.Language, returnNullOnMissing: true));
            }
            else
            {
                //The pattern key was to be looked up and wasn't in the provided values. Return null
                return null;
            }
        }
        /// <summary>
        /// Converts an object to a parameter set.
        /// </summary>
        /// <param name="data">The object to convert.</param>
        /// <param name="addWithIndex">if set to <c>true</c> values are also added by index, e.g. new { Test = 10 } will be added both as "Test" and "0".</param>
        /// <returns></returns>
        public static ParameterSet ParamsToParameterSet(object data, bool addWithIndex = false)
        {
            if( data is string )
            {
                throw new LocalizedArgumentException("ObjectHelper.ParamsToParameterSet.DataCannotBeString");
            }

            object[] array = data as object[];
            if (array != null)
            {
                var numberedValues = new Dictionary<string, object>();
                if (data != null)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        numberedValues.Add("" + i, array[i]);
                    }
                }

                return new DicitionaryParameterSet(numberedValues);
            }
            else
            {                
                var paramSet = data as ParameterSet;
                if (paramSet != null)
                {
                    return paramSet;
                }
                else
                {

                    var dictionary = data as IDictionary<string, object>;
                    if (dictionary != null)
                    {                        
                        return new DicitionaryParameterSet(dictionary);
                    }
                    else
                    {                        
                        var parameters = new DicitionaryParameterSet();                        
                        if (data != null)
                        {
                            int i = 0;

                            var attr = BindingFlags.Public | BindingFlags.Instance;
                            foreach (var property in data.GetType().GetProperties(attr))
                            {
                                if (property.CanRead)
                                {
                                    var val = property.GetValue(data, null);
                                    parameters.SetObject(property.Name, val);
                                    if (addWithIndex)
                                    {
                                        parameters.SetObject("" + i, val);
                                    }
                                    ++i;
                                }                                
                            }                            
                        }
                        return parameters;  
                    }
                }
            }
        }
        //If another namespace is needed for the pattern lookup it can be written as Namespace__Key, i.e. namespace and key seperated by two underscores '_'
        public ParameterValue GetValue(EvaluationContext context)
        {
            var values = context.Parameters;


            string actualPatternKey;

            if (PatternKey.StartsWith("@"))
            {
                var parts = PatternKey.Substring(1).Split('+');
                actualPatternKey = (string)values.GetObject(parts[0]);
                if (parts.Length > 1)
                {
                    actualPatternKey += parts[1];
                }
            }
            else
            {
                actualPatternKey = PatternKey;
            }

            string ns = context.Namespace;

            int ix = actualPatternKey.IndexOf(NamespaceQualifier);

            if (ix != -1)
            {
                ns = actualPatternKey.Substring(0, ix);
                actualPatternKey = actualPatternKey.Substring(ix + NamespaceQualifier.Length);
            }

            if (actualPatternKey != null)
            {
                int i          = 0;
                var callValues = new DicitionaryParameterSet();
                foreach (var p in Parameters)
                {
                    if ((p.Key.StartsWith("\"") || p.Key.StartsWith("'")) && (p.Key.EndsWith("\"") || p.Key.EndsWith("'")))
                    {
                        callValues["" + i] = ParameterValue.Wrap(p.Key.Substring(1, p.Key.Length - 2));
                    }
                    else
                    {
                        ParameterValue v = null;
                        if (p.Value != null)
                        {
                            v = values[p.Key].Clone();
                            v.DefaultFormat = p.Value;
                        }
                        else
                        {
                            v = values[p.Key];
                        }

                        callValues[p.Key]  = v;
                        callValues["" + i] = v;
                    }
                    ++i;
                }

                return(new UnencodedParameterValue(Manager.Get(actualPatternKey, callValues,
                                                               ns: ns, language: context.Language, returnNullOnMissing: true)));
            }
            else
            {
                //The pattern key was to be looked up and wasn't in the provided values. Return null
                return(null);
            }
        }