Пример #1
0
        public static Dictionary <string, Action <object, object> > GetSetters(Type entityType)
        {
            Dictionary <string, Action <object, object> > dictionary = null;

            if (!_objectSetters.TryGetValue(entityType, out dictionary))
            {
                var dictionary2 = _objectSetters;
                lock (dictionary2)
                {
                    if (_objectSetters.TryGetValue(entityType, out dictionary))
                    {
                        return(dictionary);
                    }
                    dictionary = new Dictionary <string, Action <object, object> >();
                    foreach (var info in ExpressionReflectorCore.GetProperties(entityType).Values)
                    {
                        if (info.GetSetMethod() != null)
                        {
                            var expression  = Expression.Parameter(ExpressionReflectorCore.ObjectType);
                            var expression2 = Expression.Parameter(ExpressionReflectorCore.ObjectType);
                            var right       = Expression.Convert(expression2, info.PropertyType);
                            ParameterExpression[] parameters = { expression, expression2 };
                            var action =
                                Expression.Lambda <Action <object, object> >(
                                    Expression.Assign(
                                        Expression.Property(Expression.Convert(expression, entityType), info), right),
                                    parameters).Compile();
                            dictionary.Add(info.Name, action);
                        }
                    }
                    _objectSetters.Add(entityType, dictionary);
                }
            }
            return(dictionary);
        }
Пример #2
0
        public static Dictionary <string, Func <object, object> > GetGetters(Type entityType)
        {
            Dictionary <string, Func <object, object> > dictionary = null;

            if (!_objectGetters.TryGetValue(entityType, out dictionary))
            {
                var dictionary2 = _objectGetters;
                lock (dictionary2)
                {
                    if (_objectGetters.TryGetValue(entityType, out dictionary))
                    {
                        return(dictionary);
                    }
                    dictionary = new Dictionary <string, Func <object, object> >();
                    foreach (var info in ExpressionReflectorCore.GetProperties(entityType).Values)
                    {
                        var expression = Expression.Parameter(ExpressionReflectorCore.ObjectType);
                        ParameterExpression[] parameters = { expression };
                        var func =
                            Expression.Lambda <Func <object, object> >(
                                Expression.Convert(
                                    Expression.Property(Expression.Convert(expression, entityType), info),
                                    ExpressionReflectorCore.ObjectType), parameters).Compile();
                        dictionary.Add(info.Name, func);
                    }
                    _objectGetters.Add(entityType, dictionary);
                }
            }
            return(dictionary);
        }
Пример #3
0
 public static IDictionary <string, PropertyInfo> GetProperties(Type type)
 {
     return(ExpressionReflectorCore.GetProperties(type));
 }