Example #1
0
        public void TestStructField()
        {
            //值类型不支持成员赋值操作
            StructEntity obj = new StructEntity(null, 0, null, 0, "C", 3, "D", 4);

            obj.ClassField  = "A";
            obj.StructField = 1;
            StructEntity.StaticClassField  = "B";
            StructEntity.StaticStructField = 2;

            Literacy lit = new Literacy(obj.GetType());

            object value;

            lit.Load.PublicField();
            //公共实例字段
            value = lit.Field["ClassField"].GetValue(obj);
            Assert.AreEqual(value, "A");

            value = lit.Field["StructField"].GetValue(obj);
            Assert.AreEqual(value, 1);

            //公共静态字段
            lit.Load.StaticField(false);
            value = lit.Field["StaticClassField"].GetValue(obj);
            Assert.AreEqual(value, "B");

            value = lit.Field["StaticStructField"].GetValue(obj);
            Assert.AreEqual(value, 2);

            //私有实例字段
            lit.Load.NonPublicField();

            value = lit.Field["PrivateClassField"].GetValue(obj);
            Assert.AreEqual(value, "C");

            value = lit.Field["PrivateStructField"].GetValue(obj);
            Assert.AreEqual(value, 3);

            //私有静态字段
            lit.Load.StaticField(true);

            value = lit.Field["PrivateStaticClassField"].GetValue(obj);
            Assert.AreEqual(value, "D");

            value = lit.Field["PrivateStaticStructField"].GetValue(obj);
            Assert.AreEqual(value, 4);
        }
Example #2
0
        public void TestStructProperty()
        {
            //值类型不支持成员赋值操作
            StructEntity obj = new StructEntity("C", 3, "D", 4, null, 0, null, 0);

            obj.ClassProperty  = "A";
            obj.StructProperty = 1;
            StructEntity.StaticClassProperty  = "B";
            StructEntity.StaticStructProperty = 2;

            Literacy lit = new Literacy(obj.GetType());

            object value;

            //公共实例属性
            value = lit.Property["ClassProperty"].GetValue(obj);
            Assert.AreEqual(value, "A");

            value = lit.Property["StructProperty"].GetValue(obj);
            Assert.AreEqual(value, 1);

            //公共静态属性
            lit.Load.StaticProperty(false);
            value = lit.Property["StaticClassProperty"].GetValue(obj);
            Assert.AreEqual(value, "B");

            value = lit.Property["StaticStructProperty"].GetValue(obj);
            Assert.AreEqual(value, 2);

            //私有实例属性
            lit.Load.NonPublicProperty();

            value = lit.Property["PrivateClassProperty"].GetValue(obj);
            Assert.AreEqual(value, "C");

            value = lit.Property["PrivateStructProperty"].GetValue(obj);
            Assert.AreEqual(value, 3);

            //私有静态属性
            lit.Load.StaticProperty(true);

            value = lit.Property["PrivateStaticClassProperty"].GetValue(obj);
            Assert.AreEqual(value, "D");

            value = lit.Property["PrivateStaticStructProperty"].GetValue(obj);
            Assert.AreEqual(value, 4);
        }
Example #3
0
        public void TestStructMethod()
        {
            StructEntity obj     = new StructEntity();
            var          methods = obj.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);

            foreach (var method in methods)
            {
                string   str  = "Z";
                int      i    = 26;
                object[] args = new object[] { str, i };

                var caller = Literacy.CreateCaller(method);
                if (method.Name.EndsWith("Ref"))
                {
                    var val = caller(obj, args);
                    obj.MethodRef(ref str, ref i);
                    Assert.AreEqual(val, i);
                }
                else if (method.Name.EndsWith("Out"))
                {
                    var val = caller(obj, args);
                    obj.MethodOut(out str, out i);
                    Assert.AreEqual(val, str);
                }
                else if (method.Name.EndsWith("Method"))
                {
                    args = obj.Method(str, i);
                    str  = (string)args[0];
                    i    = (int)args[1];
                    args = (object[])caller(obj, str, i);
                }
                else
                {
                    continue;
                }

                Assert.AreEqual(args[0], str);
                Assert.AreEqual(args[1], i);
                if (method.Name.Contains("Static") == false)
                {
                    Assert.AreEqual(obj.ClassProperty, str);
                    Assert.AreEqual(obj.StructProperty, i);
                }
            }
        }