public DynamicPropertyAccessor GetAccessor(Type type, string propertyName)
        {
            DynamicPropertyAccessor accessor;
            Dictionary <string, DynamicPropertyAccessor> typeCache;

            if (this.m_cache.TryGetValue(type, out typeCache))
            {
                if (typeCache.TryGetValue(propertyName, out accessor))
                {
                    return(accessor);
                }
            }

            lock (m_mutex)
            {
                if (!this.m_cache.ContainsKey(type))
                {
                    this.m_cache[type] = new Dictionary <string, DynamicPropertyAccessor>();
                }

                accessor = new DynamicPropertyAccessor(type, propertyName);
                this.m_cache[type][propertyName] = accessor;

                return(accessor);
            }
        }
        public void GetValueTest()
        {
            var t = new Temp { Value = null };

            PropertyInfo propertyInfo = t.GetType().GetProperty("Value");
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();
            for (var i = 0; i < 1000000; i++)
            {
                var value = propertyInfo.GetValue(t, null);
            }
            watch1.Stop();
            Trace.WriteLine("Reflection: " + watch1.Elapsed);

            DynamicPropertyAccessor property = new DynamicPropertyAccessor(t.GetType(), "Value");
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();
            for (var i = 0; i < 1000000; i++)
            {
                var value = property.GetValue(t);
            }
            watch2.Stop();
            Trace.WriteLine("Lambda: " + watch2.Elapsed);

            Stopwatch watch3 = new Stopwatch();
            watch3.Start();
            for (var i = 0; i < 1000000; i++)
            {
                var value = t.Value;
            }
            watch3.Stop();
            Trace.WriteLine("Direct: " + watch3.Elapsed);
        }
        public DynamicPropertyAccessor GetAccessor(Type type, string propertyName)
        {
            DynamicPropertyAccessor accessor;
            Dictionary<string, DynamicPropertyAccessor> typeCache;

            if (this.m_cache.TryGetValue(type, out typeCache))
            {
                if (typeCache.TryGetValue(propertyName, out accessor))
                {
                    return accessor;
                }
            }

            lock (m_mutex)
            {
                if (!this.m_cache.ContainsKey(type))
                {
                    this.m_cache[type] = new Dictionary<string, DynamicPropertyAccessor>();
                }

                accessor = new DynamicPropertyAccessor(type, propertyName);
                this.m_cache[type][propertyName] = accessor;

                return accessor;
            }
        }
Example #4
0
        public static object GetValue(object obj, string propertyName)
        {
            if (obj == null)
            {
                return(null);
            }

            Check.AssertNotNullOrEmpty(propertyName, "propertyName");

            DynamicPropertyAccessor property = new DynamicPropertyAccessor(obj.GetType(), propertyName);

            return(property.GetValue(obj));
        }
        public void SetValueTest()
        {
            int expected = 100;

            var t = new Temp { Value = null };

            PropertyInfo propertyInfo = t.GetType().GetProperty("Value");
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();
            for (var i = 0; i < 1000000; i++)
            {
                propertyInfo.SetValue(t, expected, null);
            }
            watch1.Stop();
            Trace.WriteLine("Reflection: " + watch1.Elapsed);

            Assert.AreEqual(expected, t.Value.Value);

            t.Value = 555;

            DynamicPropertyAccessor property = new DynamicPropertyAccessor(t.GetType(), "Value");
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();
            for (var i = 0; i < 1000000; i++)
            {
                property.SetValue(t,expected);
            }
            watch2.Stop();
            Trace.WriteLine("Lambda: " + watch2.Elapsed);

            Assert.AreEqual(expected, t.Value.Value);

            t.Value = 555;

            Stopwatch watch3 = new Stopwatch();
            watch3.Start();
            for (var i = 0; i < 1000000; i++)
            {
                t.Value = expected;
            }
            watch3.Stop();
            Trace.WriteLine("Direct: " + watch3.Elapsed);

            Assert.AreEqual(expected, t.Value.Value);
        }