Пример #1
0
        private static ArrayList ValidateParameterAttributes(ParameterAttribute[] parms)
        {
            ArrayList al = new ArrayList();

            if (parms == null)
            {
                al.Add(new ParameterAttribute());
                return(al);
            }

            // Validate null and dups.
            for (int i = 0; i < parms.Length; i++)
            {
                ParameterAttribute pa = parms[i];
                //PosSet ps = posSets[i];
                if (pa == null)
                {
                    continue;
                }
                if (InParameterArray(al, pa))
                {
                    throw new ArgumentException("Duplicate set name not allowed.");
                }
                al.Add(pa);
            }
            if (al.Count == 0)
            {
                al.Add(new ParameterAttribute());                 // Add default set.
            }
            return(al);
        }
Пример #2
0
 /// <summary>
 /// Returns true if SetName already exists.
 /// </summary>
 /// <param name="al"></param>
 /// <param name="parmAttribute"></param>
 /// <returns></returns>
 private static bool InParameterArray(ArrayList al, ParameterAttribute parmAttribute)
 {
     foreach (ParameterAttribute pa in al)
     {
         if (string.Compare(pa.ParameterSetName, parmAttribute.ParameterSetName, true, CultureInfo.InvariantCulture) == 0)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
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);
        }
        private static Parameter[] GetPublicReadWriteCmdMembers(object instance)
        {
            if ( instance == null )
                throw new ArgumentNullException("instance");

            ArrayList al = new ArrayList();
            Type type = instance.GetType();
            ArrayList members = new ArrayList();
            members.AddRange(type.GetProperties());
            members.AddRange(type.GetFields());

            if ( members.Count == 0 )
                throw new ArgumentException("No public members in type.");

            // TODO: Add logic to add more parms if multiple ParameterAttributes on a member.
            // Get array of ParameterAttributes.
            // Loop and add to members arraylist.
            // Probably ignore teh ParameterAttribute in CreateParameter()
            // and add a ParmAttribute to the constructor.  That way it only "sees"
            // the single ParmAttribute we pass.  Loop in case below if member
            // has ParmAttribute for each parmAttribute.  That will add that number
            // of parms to the al.
            foreach(MemberInfo mi in members)
            {
                // Only add members that have ParameterBaseAttribute(s).
                if ( ! mi.IsDefined(typeof(ParameterBaseAttribute), true) )
                    continue;

                switch(mi.MemberType)
                {
                    case MemberTypes.Property:
                        PropertyInfo pi = (PropertyInfo)mi;
                        if ( ! (pi.PropertyType.IsPublic && pi.CanRead && pi.CanWrite) )
                            throw new ArgumentException("All CMD members must be public readable and writeable.");

                        // Loop here on members if parameterAttributes.
                        object[] pArray = pi.GetCustomAttributes(typeof(ParameterAttribute), true);
                        if ( pArray != null && pArray.Length > 0 )
                        {
                            foreach(ParameterAttribute pa in pArray)
                            {
                                Parameter p = Parameter.CreateParameter(instance, mi, pa);
                                al.Add(p);
                            }
                        }
                        else
                        {
                            // Use default ParameterAttribute.
                            ParameterAttribute pa = new ParameterAttribute();
                            Parameter p = Parameter.CreateParameter(instance, mi, pa);
                            al.Add(p);
                        }
                        break;
                    case MemberTypes.Field:
                        FieldInfo fi = (FieldInfo)mi;
                        if ( ! fi.FieldType.IsPublic )
                            throw new ArgumentException("All Cmd members must be public");

                        object[] pArray2 = fi.GetCustomAttributes(typeof(ParameterAttribute), true);
                        if ( pArray2 != null && pArray2.Length > 0 )
                        {
                            foreach(ParameterAttribute pa in pArray2)
                            {
                                Parameter p = Parameter.CreateParameter(instance, mi, pa);
                                al.Add(p);
                            }
                        }
                        else
                        {
                            // Use default ParameterAttribute.
                            ParameterAttribute pa = new ParameterAttribute();
                            Parameter p = Parameter.CreateParameter(instance, mi, pa);
                            al.Add(p);
                        }

            //						//TODO
            //						Parameter p2 = Parameter.CreateParameter(instance, mi, null);
            //						al.Add(p2);
                        break;
                    default:
                        break;
                }
            }
            return (Parameter[])al.ToArray(typeof(Parameter));
        }