Example #1
0
        private static Action <IField, object> BuildFieldBinder(MetadataIdentity metadata, ColumnIdentity column)
        {
            ParameterExpression fieldParam = Expression.Parameter(typeof(IField));
            ParameterExpression valueParam = Expression.Parameter(typeof(object));

            IBindingMetadata binding = metadata.GetMetadata <IBindingMetadata>();

            Expression bindValue = valueParam;

            if (metadata != null)
            {
                Type sourceType = null;

                if (column != null)
                {
                    BindingColumnInfo columnInfo = new BindingColumnInfo()
                    {
                        Column    = column,
                        CanBeNull = true,
                        Metadata  = binding,
                    };

                    sourceType = binding.Value?.Read(columnInfo)?.ReturnType;
                }

                BindingValueInfo valueInfo = new BindingValueInfo()
                {
                    CanBeNull   = true,
                    CanBeDbNull = true,
                    Metadata    = binding,
                    Value       = bindValue,
                    SourceType  = sourceType,
                    TargetType  = binding.Type,
                };

                bindValue = binding.Value?.Convert?.Invoke(valueInfo) ?? valueParam;
            }

            MethodInfo bindMethod = typeof(IField).GetMethod(nameof(IField.Bind), new[] { typeof(object) });

            Expression bindCall = Expression.Call(fieldParam, bindMethod, Expression.Convert(bindValue, typeof(object)));

            return(Expression.Lambda <Action <IField, object> >(bindCall, fieldParam, valueParam).Compile());
        }
Example #2
0
        public BufferConverter Compile(MetadataIdentity metadata, ColumnMetadata columnInfo)
        {
            IBindingMetadata binding = metadata.Lookup <IBindingMetadata>();

            ParameterExpression inputParam     = Expression.Parameter(typeof(object));
            ParameterExpression helperParam    = Expression.Parameter(typeof(object));
            ParameterExpression helperVariable = this.GetHelperVariable(binding);

            Expression value = inputParam;

            if (binding != null)
            {
                Type sourceType = null;

                if (columnInfo != null)
                {
                    BindingColumnInfo bindingColumnInfo = new BindingColumnInfo()
                    {
                        Column    = columnInfo,
                        CanBeNull = true,
                        Metadata  = binding,
                    };

                    sourceType = binding.Value?.Read(bindingColumnInfo)?.ReturnType;
                }

                BindingValueInfo valueInfo = new BindingValueInfo()
                {
                    CanBeNull   = true,
                    CanBeDbNull = true,
                    Metadata    = binding,
                    Value       = value,
                    SourceType  = sourceType,
                    TargetType  = binding.Type,
                    Helper      = helperVariable,
                };

                try
                {
                    value = binding.Value?.Convert?.Invoke(valueInfo) ?? inputParam;
                }
                catch (BindingException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw BindingException.InvalidCast(binding, ex);
                }
            }

            value = this.GetObjectExpression(value);

            if (helperVariable != null)
            {
                Expression typedParam   = Expression.Convert(helperParam, helperVariable.Type);
                Expression assignHelper = Expression.Assign(helperVariable, typedParam);

                value = Expression.Block(new[] { helperVariable }, assignHelper, value);
            }

            BufferInternalConverter innerFunc = Expression.Lambda <BufferInternalConverter>(value, inputParam, helperParam).Compile();

            object helperObject = binding?.Helper?.Object;

            return(value => innerFunc(value, helperObject));
        }