Esempio n. 1
0
        /// ¹¹Ô캯Êý
        public XDataReader(IDataReader dataReader)
        {
            string       typeName     = dataReader.GetSchemaTable().TableName;
            DynamicClass dynamicClass = new DynamicClass(typeName);

            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                string fieldType = dataReader.GetFieldType(i).Name;
                string name      = dataReader.GetName(i);

                dynamicClass.AddProperty(fieldType, name);
                this.columnNames.Add(name);
            }

            dynamicClass.CreateAssembly();

            while (dataReader.Read())
            {
                //object[] itemArray = new object[this.columnNames.Count];
                object entity = dynamicClass.CreateInstance();

                for (int i = 0; i < this.columnNames.Count; i++)
                {
                    object value = dataReader.GetValue(i);
                    DynamicClass.SetValue(entity, columnNames[i], value);
                }

                this.rows.Add(entity);
            }
        }
Esempio n. 2
0
        public void CreateInstance()
        {
            DynamicClass dclass = new DynamicClass("Dog");
            IFunction    foo    = new DefinedFunction(null, null, null);

            dclass.SetInstanceMethod("foo", foo);

            var result = dclass.CreateInstance();

            Assert.IsNotNull(result);
            Assert.AreSame(dclass, result.Class);
            Assert.AreSame(foo, result.GetMethod("foo"));
        }
Esempio n. 3
0
        public Machine()
        {
            this.requirepaths.Add(".");
            var basicobjectclass = new DynamicClass("BasicObject", null);
            var objectclass      = new DynamicClass("Object", basicobjectclass);
            var moduleclass      = new DynamicClass("Module", objectclass);
            var classclass       = new DynamicClass("Class", moduleclass);

            this.rootcontext.SetLocalValue("BasicObject", basicobjectclass);
            this.rootcontext.SetLocalValue("Object", objectclass);
            this.rootcontext.SetLocalValue("Module", moduleclass);
            this.rootcontext.SetLocalValue("Class", classclass);

            basicobjectclass.SetClass(classclass);
            objectclass.SetClass(classclass);
            moduleclass.SetClass(classclass);
            classclass.SetClass(classclass);

            basicobjectclass.SetInstanceMethod("class", new LambdaFunction(GetClass));
            basicobjectclass.SetInstanceMethod("methods", new LambdaFunction(GetMethods));
            basicobjectclass.SetInstanceMethod("singleton_methods", new LambdaFunction(GetSingletonMethods));

            moduleclass.SetInstanceMethod("superclass", new LambdaFunction(GetSuperClass));
            moduleclass.SetInstanceMethod("name", new LambdaFunction(GetName));

            classclass.SetInstanceMethod("new", new LambdaFunction(NewInstance));

            this.rootcontext.SetLocalValue("Fixnum", new FixnumClass(this));
            this.rootcontext.SetLocalValue("Float", new FloatClass(this));
            this.rootcontext.SetLocalValue("String", new StringClass(this));
            this.rootcontext.SetLocalValue("NilClass", new NilClass(this));
            this.rootcontext.SetLocalValue("FalseClass", new FalseClass(this));
            this.rootcontext.SetLocalValue("TrueClass", new TrueClass(this));
            this.rootcontext.SetLocalValue("Array", new ArrayClass(this));
            this.rootcontext.SetLocalValue("Hash", new HashClass(this));
            this.rootcontext.SetLocalValue("Range", new RangeClass(this));

            this.rootcontext.Self = objectclass.CreateInstance();

            this.rootcontext.Self.Class.SetInstanceMethod("puts", new PutsFunction(System.Console.Out));
            this.rootcontext.Self.Class.SetInstanceMethod("print", new PrintFunction(System.Console.Out));
            this.rootcontext.Self.Class.SetInstanceMethod("require", new RequireFunction(this));
        }
Esempio n. 4
0
        public void ExecuteAssignDotCommand()
        {
            Machine machine = new Machine();
            var     @class  = new DynamicClass("Dog");
            var     method  = new DefinedFunction((new Parser("@name = name")).ParseCommand(), new string[] { "name" }, machine.RootContext);

            @class.SetInstanceMethod("name=", method);
            var nero = @class.CreateInstance();

            machine.RootContext.SetLocalValue("nero", nero);
            var leftvalue            = (DotExpression)(new Parser("nero.name")).ParseExpression();
            var value                = new ConstantExpression("Nero");
            AssignDotExpressions cmd = new AssignDotExpressions(leftvalue, value);

            var result = cmd.Evaluate(machine.RootContext);

            Assert.IsNotNull(result);
            Assert.AreEqual("Nero", result);
            Assert.AreEqual("Nero", nero.GetValue("name"));
        }
Esempio n. 5
0
        public void ExecuteAssignDotCommandWithUnknownMethod()
        {
            Machine machine = new Machine();
            var     @class  = new DynamicClass("Dog");
            var     nero    = @class.CreateInstance();

            machine.RootContext.SetLocalValue("nero", nero);
            var leftvalue            = (DotExpression)(new Parser("nero.name")).ParseExpression();
            var value                = new ConstantExpression("Nero");
            AssignDotExpressions cmd = new AssignDotExpressions(leftvalue, value);

            try
            {
                cmd.Evaluate(machine.RootContext);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(NoMethodError));
            }
        }