BindCost() public static method

Creates expression that calculates cost of conversion from arg to type target. In some cases, returned expression is a constant and can be used in compile time.
public static BindCost ( Expression arg, Type target ) : Expression
arg System.Linq.Expressions.Expression Expression to be converted.
target System.Type Target type.
return System.Linq.Expressions.Expression
Ejemplo n.º 1
0
            /// <summary>
            /// Gets expression representing cost of argument binding operation.
            /// The expression can be constant.
            /// </summary>
            public virtual Expression BindCostOf(int srcarg, Type ptype, bool ismandatory)
            {
                var key = new TmpVarKey()
                {
                    Priority = 100, ArgIndex = srcarg, TargetTypeOpt = ptype, Prefix = "costOf" + (ismandatory ? "" : "Opt")
                };

                // lookup cache
                TmpVarValue value;

                if (!_tmpvars.TryGetValue(key, out value))
                {
                    // bind cost expression
                    value = new TmpVarValue();

                    var expr_cost = ConvertExpression.BindCost(BindArgument(srcarg), ptype);
                    if (expr_cost is ConstantExpression)
                    {
                        value.Expression = expr_cost;
                    }
                    else
                    {
                        value.TrueInitializer  = expr_cost;
                        value.FalseInitializer = Expression.Constant(ismandatory ? ConversionCost.MissingArgs : ConversionCost.DefaultValue);
                        value.Expression       = Expression.Variable(typeof(ConversionCost), key.VariableName);
                    }

                    _tmpvars[key] = value;
                }

                //
                return(value.Expression);
            }