Пример #1
0
        public static Func <object> GetTypedDefaultMethod(Type type)
        {
            int hashCode = type.GetFullNameHashCode();

            if (!defaultMethodDictionary.TryGetValue(hashCode, out Func <object> value))
            {
                ConstantExpression          DefaultExpression1 = ExpressionExtensions.Default(type);
                UnaryExpression             UnaryExpression1   = Expression.Convert(DefaultExpression1, typeof(object));
                Expression <Func <object> > LambdaExpression1  = Expression.Lambda <Func <object> >(UnaryExpression1);
                value = LambdaExpression1.Compile();
                defaultMethodDictionary.Add(hashCode, value);
            }
            return(value);
        }
Пример #2
0
        public static Setter GetPropertySetter(Type type, Type valueType, PropertyInfo propertyInfo)
        {
            ParameterExpression ParameterExpression1 = Expression.Parameter(typeof(object), "obj");
            ParameterExpression ParameterExpression2 = Expression.Parameter(typeof(object), "value");
            Expression          instanceExpression;

            if (!type.IsAssignableFrom(typeof(object)))
            {
                instanceExpression = Expression.Convert(ParameterExpression1, type);
            }
            else
            {
                instanceExpression = ParameterExpression1;
            }
            Expression argumentExpression;

            if (!valueType.IsAssignableFrom(typeof(object)))
            {
                argumentExpression = Expression.Convert(ParameterExpression2, valueType);
            }
            else
            {
                argumentExpression = ParameterExpression2;
            }
            Expression MemberExpression1 = Expression.Property(instanceExpression, propertyInfo);

            if (!MemberExpression1.Type.IsNullable())
            {
                Expression          BinaryExpression1 = ExpressionExtensions.Assign(MemberExpression1, argumentExpression);
                Expression <Setter> LambdaExpression1 = Expression.Lambda <Setter>(BinaryExpression1, ParameterExpression1, ParameterExpression2);
                return(LambdaExpression1.Compile());
            }
            else
            {
                MethodInfo           methodInfo            = propertyInfo.GetSetMethod();
                MethodCallExpression MethodCallExpression1 = Expression.Call(instanceExpression, methodInfo, argumentExpression);
                Expression <Setter>  LambdaExpression1     = Expression.Lambda <Setter>(MethodCallExpression1, ParameterExpression1, ParameterExpression2);
                return(LambdaExpression1.Compile());
            }
        }