/// <summary> /// Gets the default value associated with the given <paramref name="parameter"/>. The value is /// obtained from the <see href="DefaultValueAttribute"/> if present on the parameter. This method /// does not support C# 4.0 default parameter specifications. /// </summary> /// <returns>The default value if one could be obtained and converted into the type of the parameter, /// and null otherwise.</returns> public static object DefaultValue(this ParameterInfo parameter) { var defaultValue = parameter.Attribute <DefaultValueAttribute>(); return(defaultValue != null ? Probing.TypeConverter.Get(parameter.ParameterType, defaultValue.Value) : null); }
static InjectableInfo CreateForConstructorParam( ParameterInfo paramInfo, Type enclosingType) { var injectAttr = paramInfo.Attribute <InjectAttribute>(); return(new InjectableInfo() { Optional = paramInfo.HasAttribute(typeof(InjectOptionalAttribute)), Identifier = (injectAttr == null ? null : injectAttr.Identifier), SourceName = paramInfo.Name, ContractType = paramInfo.ParameterType, EnclosingType = enclosingType, }); }
public Parameter(ParameterInfo info) { Info = info; IsOptional = info.IsOptional; var paramType = info.ParameterType; if (MondFunctionBinder.TypeCheckMap.TryGetValue(paramType, out var mondTypes)) { Type = ParameterType.Value; TypeName = mondTypes[0].GetName(); if (paramType == typeof(bool)) { Priority = 10; } else if (MondFunctionBinder.NumberTypes.Contains(paramType)) { Priority = 20; } else if (paramType == typeof(string)) { Priority = 30; } Conversion = MondFunctionBinder.MakeParameterConversion(info.ParameterType); MondTypes = mondTypes; return; } if (paramType == typeof(MondValue)) { if (info.Attribute <MondInstanceAttribute>() != null) { Type = ParameterType.Instance; TypeName = "instance"; return; } Type = ParameterType.Value; TypeName = "any"; Priority = 100; MondTypes = AnyTypes; Conversion = v => v; return; } if (paramType == typeof(MondValue[]) && info.Attribute <ParamArrayAttribute>() != null) { Type = ParameterType.Params; TypeName = "..."; Priority = 75; return; } if (paramType == typeof(MondState)) { Type = ParameterType.State; TypeName = "state"; return; } MondClassAttribute mondClass; if ((mondClass = paramType.Attribute <MondClassAttribute>()) != null) { Type = ParameterType.Value; TypeName = mondClass.Name ?? paramType.Name; MondTypes = ObjectTypes; UserDataType = info.ParameterType; #if !NO_EXPRESSIONS Conversion = v => Expression.Convert(Expression.PropertyOrField(v, "UserData"), info.ParameterType); #else Conversion = v => v.UserData; #endif return; } throw new MondBindingException(BindingError.UnsupportedType, info.ParameterType); }
/// <summary> /// Determines whether the given <paramref name="parameter"/> has an associated default value as /// supplied by an <see href="DefaultValueAttribute"/>. This method does not read the value of /// the attribute. It also does not support C# 4.0 default parameter specifications. /// </summary> /// <returns>True if the attribute was detected, false otherwise.</returns> public static bool HasDefaultValue(this ParameterInfo parameter) { var defaultValue = parameter.Attribute <DefaultValueAttribute>(); return(defaultValue != null); }