Esempio n. 1
0
        private object CodeExecuteNewObject(NewObjectExpr newObject)
        {
            try
            {
                Type t;
                // Find type in all asemblies
                if (TryFindType(newObject.ClassName, out t))
                {
                    // Params
                    object[] param;
                    if (newObject.Args.Count > 0)
                    {
                        param = new object[newObject.Args.Count];
                        for (int i = 0; i < newObject.Args.Count; i++)
                        {
                            param[i] = GenExpr(newObject.Args[i]);
                        }
                    }
                    else
                    {
                        param = null;
                    }

                    return(Activator.CreateInstance(t, param));
                }
                else
                {
                    throw new Exception("Can not be instantiated " + newObject.ClassName);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        private NewObjectExpr ParseNewObject()
        {
            NewObjectExpr newObj = new NewObjectExpr();

            newObj.ClassName = ((IdentifierToken)tokens[index]).Name;

            MoveNext();
            Eat(Tokens.LeftBracket);

            while ((tokens[index] != Tokens.RightBracket) &&
                   (index < tokens.Count))
            {
                newObj.Args.Add(ParseExpr());
                if (tokens[index] == Tokens.Comma)
                {
                    MoveNext(); // Skip comma
                }
                else if (tokens[index] == Tokens.RightBracket)
                {
                    break;
                }
                else
                {
                    throw new System.Exception("unexpected character in arg list");
                }
            }

            if (index == tokens.Count ||
                tokens[index] != Tokens.RightBracket)
            {
                throw new System.Exception("expect close bracket after open bracket/args");
            }

            // Skip RightBracket
            Eat(Tokens.RightBracket);

            return(newObj);
        }