internal static ParameterResult GetParameterResultFromParameterInfo(ParameterInfo param)
        {
            // TODO: Get parameter documentation
            var pyType = ClrModule.GetPythonType(param.ParameterType);

            string name     = param.Name;
            string typeName = PythonType.Get__name__(pyType);

            if (param.IsDefined(typeof(ParamArrayAttribute), false))
            {
                name = "*" + name;
                if (param.ParameterType.IsArray)
                {
                    var elemType = param.ParameterType.GetElementType();
                    if (elemType == typeof(object))
                    {
                        typeName = "sequence";
                    }
                    else
                    {
                        typeName = PythonType.Get__name__(DynamicHelpers.GetPythonTypeFromType(elemType)) + " sequence";
                    }
                }
            }
            else if (param.IsDefined(typeof(ParamDictionaryAttribute), false))
            {
                name     = "**" + name;
                typeName = "object";
            }

            bool isOptional = false;

            if (param.DefaultValue != DBNull.Value && !(param.DefaultValue is Missing))
            {
                name = name + " = " + PythonOps.Repr(DefaultContext.Default, param.DefaultValue);
            }
            else if (param.IsOptional)
            {
                object missing = CompilerHelpers.GetMissingValue(param.ParameterType);
                if (missing != Missing.Value)
                {
                    name = name + " = " + PythonOps.Repr(DefaultContext.Default, missing);
                }
                else
                {
                    isOptional = true;
                }
            }

            return(new ParameterResult(name, "", typeName, isOptional));
        }
Esempio n. 2
0
        internal protected override Expression ToExpression(ParameterBinder parameterBinder, IList <Expression> parameters, bool[] hasBeenUsed)
        {
            object val = ParameterInfo.DefaultValue;

            if (val is Missing)
            {
                val = CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType);
            }

            if (ParameterInfo.ParameterType.IsByRef)
            {
                return(Ast.Constant(val, ParameterInfo.ParameterType.GetElementType()));
            }

            return(parameterBinder.ConvertExpression(Ast.Constant(val), ParameterInfo, ParameterInfo.ParameterType));
        }
Esempio n. 3
0
        public override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed)
        {
            object value = ParameterInfo.GetDefaultValue();

            if (value is Missing)
            {
                value = CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType);
            }

            if (ParameterInfo.ParameterType.IsByRef)
            {
                return(AstUtils.Constant(value, ParameterInfo.ParameterType.GetElementType()));
            }

            var metaValue = new DynamicMetaObject(AstUtils.Constant(value), BindingRestrictions.Empty, value);

            return(resolver.Convert(metaValue, CompilerHelpers.GetType(value), ParameterInfo, ParameterInfo.ParameterType));
        }
Esempio n. 4
0
        protected internal override Func <object[], object> ToDelegate(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed)
        {
            if (ParameterInfo.ParameterType.IsByRef)
            {
                return(null);
            }
            else if (ParameterInfo.DefaultValue is Missing && CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType) is Missing)
            {
                // reflection throws when we do this
                return(null);
            }

            object val = ParameterInfo.DefaultValue;

            if (val is Missing)
            {
                val = CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType);
            }
            Debug.Assert(val != Missing.Value);
            return((_) => val);
        }