Exemple #1
0
        internal static Parameter CreateParameter(
            string name,
            Type type,
            object value,
            object instance,
            bool mandatory,
            bool isSwitch,
            ValidateLengthAttribute valLen,
            IntRange valCount,
            Range valRange,
            string[] valSet,
            bool varList)
        {
            Parameter p = CreateParameter(name, type, value, instance, mandatory);

            p.IsSwitch = isSwitch;
            p.ValLen   = valLen;
            p.ValCount = valCount;
            p.ValRange = valRange;
            p.ValSet   = valSet;
            p.ValueFromRemainingArguments = varList;
            return(p);
        }
Exemple #2
0
        /// <summary>
        /// Returns an instance of a Parameter object.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="mi"></param>
        /// <param name="parameterAttribute"></param>
        /// <returns></returns>
        internal static Parameter CreateParameter(object instance, MemberInfo mi, ParameterAttribute parameterAttribute)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (mi == null)
            {
                throw new ArgumentNullException("mi");
            }
            if (parameterAttribute == null)
            {
                throw new ArgumentNullException("parameterAttribute");
            }

            Type type = null;

            if (mi is PropertyInfo)
            {
                PropertyInfo pi = (PropertyInfo)mi;
                type = pi.PropertyType;
            }
            else
            {
                FieldInfo fi = (FieldInfo)mi;
                type = fi.FieldType;
            }

            Parameter p = new Parameter(mi.Name, type, null, instance, false);

            p.memberInfo = mi;

            ArrayList attrList = new ArrayList();

            ParameterBaseAttribute[] parmAttributes = GetParameterBaseAttributes(mi);
            if (parmAttributes == null)
            {
                throw new ArgumentException("No ParameterAttributes exist for member.");
            }

            // Handle ParameterAttribute special.
            if (parameterAttribute.ValueFromRemainingArguments && p.Type != typeof(string[]))
            {
                throw new CmdException("Parameter must be string[] to use ValueFromRemainingArguments.");
            }
            p.mandatory = parameterAttribute.Mandatory;
            p.setName   = parameterAttribute.ParameterSetName;
            p.position  = parameterAttribute.Position;
            p.varList   = parameterAttribute.ValueFromRemainingArguments;

            foreach (ParameterBaseAttribute pa in parmAttributes)
            {
                switch (pa.GetType().Name)
                {
                case "PromptAttribute":
                    PromptAttribute ppsa = (PromptAttribute)pa;
                    p.PromptString  = ppsa.Prompt;
                    p.DefaultAnswer = ppsa.DefaultAnswer;
                    attrList.Add(pa);
                    break;

                case "ParameterAttribute":
                    // Ignore as we handle above.
//						ParameterAttribute pa1 = (ParameterAttribute)pa;
//						p.mandatory = pa1.Mandatory;
//						p.setName = pa1.ParameterSetName;
//						p.position = pa1.Position;
//						if ( pa1.ValueFromRemainingArguments && p.Type != typeof(string[]) )
//							throw new CmdException("Parameter must be string[] to use ValueFromRemainingArguments.");
//						p.varList = pa1.ValueFromRemainingArguments;
//						attrList.Add(pa1);
                    break;

                case "HelpAttribute":
                    p.help = pa as HelpAttribute;
                    attrList.Add(p.help);
                    break;

                case "ValidateLengthAttribute":
                    if (type != typeof(string))
                    {
                        throw new CmdException("ValidateLength attribute only valid on [string] type.");
                    }
                    ValidateLengthAttribute vla = (ValidateLengthAttribute)pa;
                    p.ValLen = vla;
                    attrList.Add(pa);
                    break;

                case "ValidateCountAttribute":
                    ValidateCountAttribute vca = (ValidateCountAttribute)pa;
                    IntRange irvc = new IntRange(vca.Min, vca.Max);
                    p.ValCount = irvc;
                    attrList.Add(pa);
                    break;

                case "ValidateRangeAttribute":
                    ValidateRangeAttribute vra = (ValidateRangeAttribute)pa;
                    Type elType1;
                    if (type.IsArray)
                    {
                        elType1 = type.GetElementType();
                    }
                    else
                    {
                        elType1 = type;
                    }
                    if (!IsComparable(elType1))
                    {
                        throw new CmdException("Type must implement IComparable to use the ValidationRangeAttribute.");
                    }
                    p.ValRange = new Range(elType1, vra.MinRange.ToString(), vra.MaxRange.ToString());
                    attrList.Add(pa);
                    break;

                case "ValidateSetAttribute":
                    ValidateSetAttribute vsa = (ValidateSetAttribute)pa;
                    Type elType2;
                    if (type.IsArray)
                    {
                        elType2 = type.GetElementType();
                    }
                    else
                    {
                        elType2 = type;
                    }
                    if (!IsComparable(elType2))
                    {
                        throw new CmdException("Type must implement IComparable to use the ValidationSetAttribute.");
                    }
                    Type toType = Type.GetType(elType2.ToString() + "[]");
                    // Split string into array of target type.  ValSet will be array.
                    Array arr = (Array)Converter.ConvertFromString(vsa.SetString, toType);
                    p.ValSet = arr;
                    if (vsa.CaseInsensitive)
                    {
                        p.valSetCaseInsensitive = true;
                    }
                    attrList.Add(pa);
                    break;

                case "ValidatePatternAttribute":
                    ValidatePatternAttribute vpa = (ValidatePatternAttribute)pa;
                    Type elType;
                    if (p.Type.IsArray)
                    {
                        elType = type.GetElementType();
                    }
                    else
                    {
                        elType = type;
                    }
                    if (elType != typeof(string))
                    {
                        throw new CmdException("ValidationPattern attribute only valid on [string] type.");
                    }
                    p.valPattern = vpa.Pattern;
                    attrList.Add(pa);
                    break;

                case "SwitchAttribute":
                    if (type != typeof(bool))
                    {
                        throw new CmdException("SwitchParameter attribute only valid on [bool] type.");
                    }
                    p.IsSwitch = true;
                    attrList.Add(pa);
                    break;

                default:
                    break;
                }
            }

            return(p);
        }