Ejemplo n.º 1
0
        public void SafeFieldPerformanceTests()
        {
            int       runs               = 10000000;
            object    myClass            = new MyClassWithPrivateFields(123, new object(), 456, new object());
            FieldInfo fieldClassRefValue = IField(myClass.GetType(), "instanceRefValue");
            FieldInfo fieldClassVtValue  = IField(myClass.GetType(), "instanceVTValue");

            StopWatch stopWatch = new StopWatch();

            using (stopWatch.Start("Duration Class Set/Get field value: {0}"))
            {
                for (int i = 0; i < runs; i++)
                {
                    SafeField instanceRefField = new SafeField(fieldClassRefValue);
                    SafeField instanceVtField  = new SafeField(fieldClassVtValue);

                    int    res  = (int)instanceVtField.GetValue(myClass);
                    object ores = instanceRefField.GetValue(myClass);
                }
            }

            object    myStruct            = new MyStructWithPrivateFields(123, new object(), 456, new object());
            FieldInfo fieldStructRefValue = IField(myStruct.GetType(), "instanceRefValue");
            FieldInfo fieldStructVtValue  = IField(myStruct.GetType(), "instanceVTValue");

            using (stopWatch.Start("Duration Struct Set/Get field value: {0}"))
            {
                for (int i = 0; i < runs; i++)
                {
                    SafeField instanceRefField = new SafeField(fieldStructRefValue);
                    SafeField instanceVtField  = new SafeField(fieldStructVtValue);

                    int    res  = (int)instanceVtField.GetValue(myStruct);
                    object ores = instanceRefField.GetValue(myStruct);
                }
            }

            /* on my machine prints
             *
             * with System.Reflection.Emit.DynamicMethod generated code:
             *  Duration Class Set/Get field value: 00:00:03.2031250
             *  Duration Struct Set/Get field value: 00:00:03.5625000
             *
             * with standard reflection:
             *  Duration Class Set/Get field value: 00:00:45.4218750
             *  Duration Struct Set/Get field value: 00:00:44.5312500
             *
             */
        }
Ejemplo n.º 2
0
        public void TestInstanceMembersOfClass()
        {
            object testref1 = new object();
            object testref2 = new object();
            MyClassWithPrivateFields myClass;

            // ro readonly int
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceReadonlyVtField = new SafeField(IField(myClass.GetType(), "instanceReadonlyVTValue"));

            Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myClass));
            try { instanceReadonlyVtField.SetValue(myClass, 10); }
            catch (InvalidOperationException) { }

            // ro readonly object
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceReadonlyRefField = new SafeField(IField(myClass.GetType(), "instanceReadonlyRefValue"));

            Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myClass));
            try { instanceReadonlyRefField.SetValue(myClass, this); }
            catch (InvalidOperationException) { }

            // rw int
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceVtField = new SafeField(IField(myClass.GetType(), "instanceVTValue"));

            Assert.AreEqual(456, instanceVtField.GetValue(myClass));
            instanceVtField.SetValue(myClass, 9182);
            Assert.AreEqual(9182, instanceVtField.GetValue(myClass));

            // rw object
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceRefField = new SafeField(IField(myClass.GetType(), "instanceRefValue"));

            Assert.AreSame(testref2, instanceRefField.GetValue(myClass));
            instanceRefField.SetValue(myClass, testref1);
            Assert.AreSame(testref1, instanceRefField.GetValue(myClass));
        }
Ejemplo n.º 3
0
 public void TestStaticMembersOfClass()
 {
     TestStaticMembersOf(typeof(MyClassWithPrivateFields), MyClassWithPrivateFields.GetStaticReadonlyRefValue());
 }
Ejemplo n.º 4
0
        public void SafeFieldPerformanceTests()
        {
            int runs = 10000000;
            object myClass = new MyClassWithPrivateFields(123, new object(), 456, new object());
            FieldInfo fieldClassRefValue = IField(myClass.GetType(), "instanceRefValue");
            FieldInfo fieldClassVtValue = IField(myClass.GetType(), "instanceVTValue");

            StopWatch stopWatch = new StopWatch();
            using (stopWatch.Start("Duration Class Set/Get field value: {0}"))
            {
                for(int i=0;i<runs;i++)
                {
                    SafeField instanceRefField = new SafeField(fieldClassRefValue);
                    SafeField instanceVtField = new SafeField(fieldClassVtValue);
            
                    int res = (int) instanceVtField.GetValue( myClass );
                    object ores = instanceRefField.GetValue( myClass );
                }
            }

            object myStruct = new MyStructWithPrivateFields(123, new object(), 456, new object());
            FieldInfo fieldStructRefValue = IField(myStruct.GetType(), "instanceRefValue");
            FieldInfo fieldStructVtValue = IField(myStruct.GetType(), "instanceVTValue");
            
            using (stopWatch.Start("Duration Struct Set/Get field value: {0}"))
            {
                for(int i=0;i<runs;i++)
                {
                    SafeField instanceRefField = new SafeField(fieldStructRefValue);
                    SafeField instanceVtField = new SafeField(fieldStructVtValue);

                    int res = (int) instanceVtField.GetValue( myStruct );
                    object ores = instanceRefField.GetValue( myStruct );
                }
            }

            /* on my machine prints
                
             with System.Reflection.Emit.DynamicMethod generated code:
                Duration Class Set/Get field value: 00:00:03.2031250
                Duration Struct Set/Get field value: 00:00:03.5625000
             
             with standard reflection:
                Duration Class Set/Get field value: 00:00:45.4218750
                Duration Struct Set/Get field value: 00:00:44.5312500
             
             */
        }
Ejemplo n.º 5
0
        public void TestInstanceMembersOfClass()
        {
            object testref1 = new object();
            object testref2 = new object();
            MyClassWithPrivateFields myClass;

            // ro readonly int
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceReadonlyVtField = new SafeField(IField(myClass.GetType(), "instanceReadonlyVTValue"));
            Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myClass));
            try { instanceReadonlyVtField.SetValue(myClass, 10); }
            catch (InvalidOperationException) { }

            // ro readonly object
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceReadonlyRefField = new SafeField(IField(myClass.GetType(), "instanceReadonlyRefValue"));
            Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myClass));
            try { instanceReadonlyRefField.SetValue(myClass, this); }
            catch (InvalidOperationException) { }

            // rw int
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceVtField = new SafeField(IField(myClass.GetType(), "instanceVTValue"));
            Assert.AreEqual(456, instanceVtField.GetValue(myClass));
            instanceVtField.SetValue(myClass, 9182);
            Assert.AreEqual(9182, instanceVtField.GetValue(myClass));

            // rw object
            myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
            SafeField instanceRefField = new SafeField(IField(myClass.GetType(), "instanceRefValue"));
            Assert.AreSame(testref2, instanceRefField.GetValue(myClass));
            instanceRefField.SetValue(myClass, testref1);
            Assert.AreSame(testref1, instanceRefField.GetValue(myClass));
        }