public void CtorFieldInfo()
 {
     var fieldInfo = typeof(ComplexType).GetField(nameof(ComplexType.value));
     var getterAndSetter = new GetterAndSetter<ComplexType, int>(fieldInfo);
     var complexType = new ComplexType();
     getterAndSetter.SetValue(complexType, 1);
     Assert.AreEqual(1, complexType.Value);
     Assert.AreEqual(1, getterAndSetter.GetValue(complexType));
 }
Beispiel #2
0
        public void CtorFieldInfo()
        {
            var fieldInfo       = typeof(ComplexType).GetField(nameof(ComplexType.value));
            var getterAndSetter = new GetterAndSetter <ComplexType, int>(fieldInfo);
            var complexType     = new ComplexType();

            getterAndSetter.SetValue(complexType, 1);
            Assert.AreEqual(1, complexType.Value);
            Assert.AreEqual(1, getterAndSetter.GetValue(complexType));
        }
Beispiel #3
0
        public void CreateFromPropertyInfo()
        {
            var propertyInfo    = typeof(ComplexType).GetProperty(nameof(ComplexType.Value));
            var getterAndSetter = (GetterAndSetter <ComplexType, int>)GetterAndSetter.GetOrCreate(propertyInfo);
            var complexType     = new ComplexType();

            getterAndSetter.SetValue(complexType, 1);
            Assert.AreEqual(1, complexType.Value);
            Assert.AreEqual(1, getterAndSetter.GetValue(complexType));
        }
Beispiel #4
0
        public void CtorFieldInfo()
        {
            var type = typeof(ComplexType);

            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var getterAndSetter = new GetterAndSetter <ComplexType, int>(field);
                //var complexType = new ComplexType();
                //getterAndSetter.SetValue(complexType, 1);
                //Assert.AreEqual(1, complexType.Value);
                //Assert.AreEqual(1, getterAndSetter.GetValue(complexType));
            }
        }
Beispiel #5
0
 public void CtorFieldInfo()
 {
     var type = typeof(ComplexType);
     foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
     {
         var getterAndSetter = new GetterAndSetter<ComplexType, int>(field);
         //var complexType = new ComplexType();
         //getterAndSetter.SetValue(complexType, 1);
         //Assert.AreEqual(1, complexType.Value);
         //Assert.AreEqual(1, getterAndSetter.GetValue(complexType));
     }
 }
    private static void EnsurePropertySettersAndGettersForType(Type type)
    {
        if (false == _compiledProperties.ContainsKey(type))
        {
            lock (_compiledPropertiesLockObject)
                if (false == _compiledProperties.ContainsKey(type))
                {
                    Dictionary <string, GetterAndSetter> _getterAndSetters;
                    _compiledProperties[type] = _getterAndSetters = new Dictionary <string, GetterAndSetter>();

                    var properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);

                    foreach (var property in properties)
                    {
                        var getterAndSetter = new GetterAndSetter();
                        _getterAndSetters[property.Name] = getterAndSetter;

                        // burn getter and setter

                        if (property.CanRead)
                        {
                            // burn getter

                            var param = Expression.Parameter(typeof(object), "param");

                            Expression propExpression = Expression.Convert(Expression.Property(Expression.Convert(param, type), property), typeof(object));

                            var lambda = Expression.Lambda(propExpression, new[] { param });

                            var compiled = lambda.Compile() as Func <object, object>;
                            getterAndSetter.Getter = compiled;
                        }

                        if (property.CanWrite)
                        {
                            var thisParam = Expression.Parameter(typeof(Object), "this");
                            var theValue  = Expression.Parameter(typeof(Object), "value");

                            var isValueType = property.PropertyType.IsClass == false && property.PropertyType.IsInterface == false;

                            Expression valueExpression;
                            if (isValueType)
                            {
                                valueExpression = Expression.Unbox(theValue, property.PropertyType);
                            }
                            else
                            {
                                valueExpression = Expression.Convert(theValue, property.PropertyType);
                            }

                            var thisExpression = Expression.Property(Expression.Convert(thisParam, type), property);


                            Expression body = Expression.Assign(thisExpression, valueExpression);

                            var block = Expression.Block(new[]
                            {
                                body,
                                Expression.Empty()
                            });

                            var lambda = Expression.Lambda(block, thisParam, theValue);

                            getterAndSetter.Setter = lambda.Compile() as Action <Object, Object>;
                        }
                    }
                }
        }
    }