Exemple #1
0
        public void TestAccessField_Type()
        {
            IReflector r = Reflector.Bind(typeof(ReflectSampleClass1), ReflectorPolicy.TypeAll);

            Assert.IsTrue(r.ExistField("V"));
            Assert.IsFalse(r.ExistField("v"));
        }
Exemple #2
0
        public void TestAccessField_InstancePrivate()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstanceAll);
            Type t = r.GetFieldType("_name");

            Assert.IsTrue(t == typeof(string));

            object val = r.GetFieldValue("_name");

            Assert.AreEqual("sample1", val);

            Assert.IsTrue(r.ExistField("_name"));
            Assert.IsFalse(r.ExistField("_Name"));

            Assert.IsFalse(r.ExistField("V"));
        }
Exemple #3
0
        public void TestAccessField_InstancePublic()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstancePublic);
            Type t = r.GetFieldType("X");

            Assert.IsTrue(t == typeof(int));

            object val = r.GetFieldValue("X");

            Assert.AreEqual(5, val);

            Assert.IsTrue(r.ExistField("X"));
            Assert.IsFalse(r.ExistField("x"));

            r.SetFieldValue("X", 10);
            Assert.AreEqual(10, o.X);
        }
Exemple #4
0
        public override object Visit(IndexerNode obj)
        {
            object instance = obj.Variable.Accept(this);

            object[] paramArray = (object[])obj.Args.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            ISmartInvoker   invoker;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                invoker = EvalSmartInvoker.CreateInstance(cdef.ObjType, false);
            }
            else
            {
                invoker = EvalSmartInvoker.CreateInstance(instance, true);
            }

            if (instance.GetType().IsArray)//Note:invoke "Get" on array
            {
                return(invoker.Invoke("Get", paramArray));
            }
            else//Note:invoke indexer on instance
            {
                try//Note:Try indexer first
                {
                    return(invoker.Invoke("get_Item", paramArray));
                }
                catch//Note:then try property
                {
                    if (paramArray.Length == 1 && paramArray[0] != null)
                    {
                        string     propertyName = paramArray[0].ToString();
                        IReflector r            = Reflector.Bind(instance);
                        if (r.ExistProperty(propertyName))
                        {
                            return(r.GetPropertyValue(propertyName));
                        }
                        else if (r.ExistField(propertyName))
                        {
                            return(r.GetFieldValue(propertyName));
                        }
                        else
                        {
                            ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName);
                        }
                    }
                    //Note:Can not convert to property, throw
                    throw;
                }
            }
        }