예제 #1
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;
            }
예제 #2
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] + "'");
                    }
                }
            }
예제 #3
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;
            }