예제 #1
0
        private object DoPy(object target, object[] args)
        {
            if (args.Length > 0)
            {
                PySnippet snippet = new PySnippet(args[0].ToString());
                object[]  newargs = new object[args.Length - 1];

                Array.Copy(args, 1, newargs, 0, args.Length - 1);

                return(snippet.Invoke(target, null, newargs));
            }
            else
            {
                return(null);
            }
        }
예제 #2
0
        void EvaluateFunction(object target, string name, FunctionArgs args, Dictionary <string, object> extras, bool caseSensitive)
        {
            if (!caseSensitive)
            {
                name = name.ToLowerInvariant();
            }

            // If a dotted name then do a slow evaluation, for functions we can only have a list of properties then a function call
            if (!_funcs.ContainsKey(name) && name.Contains("."))
            {
                string[] parts = name.Split('.');
                object   ret   = EvaluateParameter(target, parts[0], extras, caseSensitive);

                for (int i = 1; i < parts.Length - 1; ++i)
                {
                    ret = GetValue(ret, parts[i], caseSensitive);
                }

                object[] oa = args.EvaluateParameters();

                Type type = ret as Type;

                if ((type != null) && typeof(PySnippet).IsAssignableFrom(type))
                {
                    PySnippet snippet = (PySnippet)Activator.CreateInstance(type);

                    _funcs.Add(name, (o, a) => snippet.Invoke(o, parts[parts.Length - 1], a));

                    args.Result = _funcs[name](target, oa);
                }
                else
                {
                    ret = ret.GetType();
                    Type[] ta = new Type[oa.Length];

                    for (int i = 0; i < oa.Length; ++i)
                    {
                        ta[i] = oa[i].GetType();
                    }

                    MethodInfo method = null;
                    try
                    {
                        method = type.GetMethod(parts[parts.Length - 1], GetFlags(caseSensitive), null, ta, null);
                    }
                    catch (AmbiguousMatchException)
                    {
                    }

                    if (method == null)
                    {
                        throw new ArgumentException(String.Format(Properties.Resources.ExpressionResolver_CannotFuncFunction, name));
                    }

                    args.Result = method.Invoke(ret, oa);
                }
            }
            else
            {
                if (!_funcs.ContainsKey(name))
                {
                    if (_targetType == null)
                    {
                        throw new ArgumentException(String.Format(Properties.Resources.ExpressionResolver_CannotFuncFunction, name));
                    }

                    Type baseType = _targetType;

                    MethodInfo method = baseType.GetMethod(name, GetFlags(caseSensitive));
                    if (method == null)
                    {
                        // Not a method, try a type name which we expect to be a script
                        Type type = baseType.Assembly.GetType(name);

                        if ((type == null) || !typeof(PySnippet).IsAssignableFrom(type))
                        {
                            throw new ArgumentException(String.Format(Properties.Resources.ExpressionResolver_CannotFuncFunction, name));
                        }

                        PySnippet snippet = (PySnippet)Activator.CreateInstance(type);

                        _funcs.Add(name, (o, a) => snippet.Invoke(o, null, a));
                    }
                    else
                    {
                        _funcs.Add(name, (o, a) => method.Invoke(o, a));
                    }
                }

                args.Result = _funcs[name](target, args.EvaluateParameters());
            }
        }
예제 #3
0
        private object DoPy(object target, object[] args)
        {
            if (args.Length > 0)
            {
                PySnippet snippet = new PySnippet(args[0].ToString());
                object[] newargs = new object[args.Length - 1];

                Array.Copy(args, 1, newargs, 0, args.Length - 1);

                return snippet.Invoke(target, null, newargs);
            }
            else
            {
                return null;
            }
        }