Пример #1
0
            public void SetValue(IScriptExpressionNameResolver nameResolver, object value)
            {
                BindingFlags flags = BindingFlags.FlattenHierarchy |
                                     BindingFlags.Public |
                                     BindingFlags.Instance;

                object obj = nameResolver.ResolveName(_parts[0]);

                for (int i = 1; i < _parts.Length; i++) {
                    PropertyInfo pi = obj.GetType().GetProperty(_parts[i], flags);
                    if (pi != null) {
                        if (i == _parts.Length - 1) {
                            if ((value == null) || (pi.PropertyType.IsAssignableFrom(value.GetType()) == false)) {
                                value = Convert.ChangeType(value, pi.PropertyType, CultureInfo.CurrentCulture);
                            }

                            pi.SetValue(obj, value, null);
                        }
                        else {
                            obj = pi.GetValue(obj, null);
                        }
                    }
                    else {
                        throw new InvalidOperationException("Unknown property '" + _parts[i] + "'");
                    }
                }
            }
Пример #2
0
            public object GetValue(IScriptExpressionNameResolver nameResolver)
            {
                object value = nameResolver.ResolveName(_parts[0]);

                if (_parts.Length != 1) {
                    BindingFlags flags = BindingFlags.FlattenHierarchy |
                                         BindingFlags.Public |
                                         BindingFlags.Instance;

                    for (int i = 1; i < _parts.Length; i++) {
                        PropertyInfo pi = value.GetType().GetProperty(_parts[i], flags);
                        if (pi != null) {
                            value = pi.GetValue(value, null);
                        }
                        else {
                            throw new InvalidOperationException("Unknown property '" + _parts[i] + "'");
                        }
                    }
                }

                return value;
            }
Пример #3
0
 public object GetValue(IScriptExpressionNameResolver nameResolver)
 {
     return _value;
 }
Пример #4
0
            public object GetValue(IScriptExpressionNameResolver nameResolver)
            {
                object value = null;

                BindingFlags flags = BindingFlags.FlattenHierarchy |
                                     BindingFlags.Public |
                                     BindingFlags.Instance;

                object obj = nameResolver.ResolveName(_parts[0]);

                for (int i = 1; i < _parts.Length; i++) {
                    if (i == _parts.Length - 1) {
                        MethodInfo mi = obj.GetType().GetMethod(_parts[i], flags);
                        if (mi == null) {
                            throw new InvalidOperationException("Unknown method '" + _parts[i] + "'");
                        }

                        object[] paramValues = null;

                        if (_parameters != null) {
                            ParameterInfo[] paramInfos = mi.GetParameters();
                            if (_parameters.Count != paramInfos.Length) {
                                throw new InvalidOperationException("Parameter count mismatch for method '" + _parts[i] + "'");
                            }

                            paramValues = new object[_parameters.Count];

                            for (int j = 0; j < paramValues.Length; j++) {
                                object paramValue = _parameters[j].GetValue(nameResolver);
                                if ((paramValue == null) || (paramInfos[j].ParameterType.IsAssignableFrom(paramValue.GetType()) == false)) {
                                    paramValue = Convert.ChangeType(paramValue, paramInfos[j].ParameterType, CultureInfo.CurrentCulture);
                                }

                                paramValues[j] = paramValue;
                            }
                        }

                        value = mi.Invoke(obj, paramValues);
                    }
                    else {
                        PropertyInfo pi = obj.GetType().GetProperty(_parts[i], flags);
                        if (pi != null) {
                            obj = pi.GetValue(obj, null);
                        }
                        else {
                            throw new InvalidOperationException("Unknown property '" + _parts[i] + "'");
                        }
                    }
                }

                return value;
            }
Пример #5
0
        /// <summary>
        /// Executes a script expression.
        /// </summary>
        /// <param name="nameResolver">An object that can resolve names in the script expression to object instances.</param>
        /// <returns>The resulting value from executing the expression if any.</returns>
        public object Execute(IScriptExpressionNameResolver nameResolver)
        {
            object value = _rhs.GetValue(nameResolver);
            if (_lhs != null) {
                ((PropertyAccessExpression)_lhs).SetValue(nameResolver, value);
            }

            return value;
        }