/// <summary> /// Builds a method binding argument for the specified argument of the specified type /// </summary> /// <param name="argumentName"></param> /// <param name="type"></param> /// <param name="bindingType"></param> /// <returns></returns> public static IMethodBindingArgument BuildArgumentOfType(string argumentName, Type type, Enum bindingType) { if (bindingType is ChangeableMethodBindingArgumentType changable) { switch (bindingType) { case ChangeableMethodBindingArgumentType.Static: return(new StaticMethodBindingArgument(argumentName, GetDefault.GetDefaultValueFromType(type))); case ChangeableMethodBindingArgumentType.Memory: return(new MemoryMethodBindingArgument(argumentName)); case ChangeableMethodBindingArgumentType.Argument: return(new ArgumentMethodBindingArgument(argumentName)); } } else if (bindingType is NonChangeableMethodBindingType nonChangeable) { switch (bindingType) { case NonChangeableMethodBindingType.Params: return(new ArgumentMethodBindingArgument(argumentName)); } } throw new ArgumentException($"Unknown binding type {bindingType}"); }
public StaticMethodBindingArgument(ParameterInfo info) { this.argName = info.Name; if (info.ParameterType == typeof(string)) { this.ArgValue = ""; } else { this.ArgValue = GetDefault.GetDefaultValueFromType(info.ParameterType); } }
public void Validate(MethodBindingValidationContext validation) { this.type = validation.GetTypeOfArgument(ArgName); if (ArgValue == null) { if (type != null) { if (type == typeof(string)) { ArgValue = ""; } else if (type.IsPrimitive || type.IsValueType) { ArgValue = GetDefault.GetDefaultValueFromType(type); } } } }
/// <summary> /// Create a new method binding from the givin method info /// </summary> /// <param name="info"></param> public MethodBinding(MethodInfo info) { this.className = info.DeclaringType.ToString(); this.methodName = info.Name; arguments = info .GetParameters() .Select( x => { if (x.GetCustomAttribute <ParamArrayAttribute>() != null) { return((IMethodBindingArgument) new ParamsMethodBindingArgument(x)); } else { return((IMethodBindingArgument) new StaticMethodBindingArgument(x.Name, GetDefault.GetDefaultValueFromType(x.ParameterType))); } }) .ToArray(); PruneArgs(); }