排列组合

/*------------------------------------------------------------------------------
 *  文件名:     Type2ValueConvert.cs
 *  功能說明:   用于创建将类型转换为可能的数据数组
 *
 *  創建人:    it_Eric 
 *  創建時間:   10/30/2013 10:15:20 AM
 *   
 *  修改人:    
 *  修改說明:    
 * 
*-----------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace netsystemsautotest
{
    /// <summary>
    /// 类型和值的转换类
    /// </summary>
    public class Type2ValueConvert
    {
        private Type _type;
        public Type2ValueConvert(Type type)
        {
            this._type = type;
        }

        /// <summary> 
        /// 传入对应的类型返回默认值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object Type2Value()
        {
            if (_type == typeof(int))
            {
                return list2object<int>(Int2Value());
            }
            else if (_type == typeof(uint))
            {
                return list2object<uint>(UInt2Value());
            }
            else if (_type == typeof(short))
            {
                return list2object<short>(Short2Value());
            }
            else if (_type == typeof(ushort))
            {
                return list2object<ushort>(UShort2Value());
            }
            else if (_type == typeof(long))
            {
                return list2object<long>(Long2Value());
            }
            else if (_type == typeof(ulong))
            {
                return list2object<ulong>(ULong2Value());
            }
            else if (_type == typeof(double))
            {
                return list2object<double>(Double2Value());
            }
            else if (_type == typeof(float))
            {
                return list2object<float>(Float2Value());
            }
            else if (_type == typeof(string))
            {
                return list2object<string>(String2Value());
            }
            else if (_type == typeof(bool))
            {
                return list2object<bool>(Bool2Value());
            }
            else if (_type == typeof(char))
            {
                return list2object<char>(Char2Value());
            }
            else if (_type == typeof(byte))
            {
                return list2object<byte>(Byte2Value());
            }
            else if (_type == typeof(DateTime))
            {
                return list2object<DateTime>(Datetimes2Value());
            }
            else
            {
                return Object2Value();
            }
        }

        private List<object> list2object<T>(List<T> lst)
        {
            List<object> result = new List<object>();
            foreach (T item in lst)
            {
                result.Add(item);
            }
            return result;
        }

        private List<DateTime> Datetimes2Value()
        {
            List<DateTime> lst = new List<DateTime>();
            lst.Add(DateTime.Now);
            lst.Add(DateTime.Today);
            lst.Add(DateTime.MaxValue);
            lst.Add(DateTime.MinValue);
            return lst;
        }

        private List<object> Object2Value()
        {
            List<object> lst = new List<object>();
            lst.Add(null);
            return lst;
        }

        private List<byte> Byte2Value()
        {
            List<byte> lst = new List<byte>();
            lst.Add(byte.MaxValue);
            lst.Add(byte.MinValue);
            lst.Add(default(byte));
            return lst;
        }

        private List<char> Char2Value()
        {
            List<char> lst = new List<char>();
            lst.Add((char)new Random().Next(33, 126));
            return lst;
        }

        private List<bool> Bool2Value()
        {
            List<bool> lst = new List<bool>();
            lst.Add(true);
            lst.Add(false);
            return lst;
        }

        private List<string> String2Value()
        {
            List<string> lst = new List<string>();
            lst.Add(string.Empty);
            lst.Add(null);
            lst.Add("abcdefghijklmnopqrstuvwxyz");
            lst.Add("abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc" +
                    "defghijklmnopqrstuvwxyzzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
            return lst;
        }

        private List<float> Float2Value()
        {
            List<float> lst = new List<float>();
            lst.Add(float.MaxValue);
            lst.Add(float.MinValue);
            lst.Add(0);
            lst.Add((float)new Random().Next(int.MinValue, int.MaxValue));
            return lst;
        }

        private List<double> Double2Value()
        {
            List<double> lst = new List<double>();
            lst.Add(double.MaxValue);
            lst.Add(double.MinValue);
            lst.Add(0);
            lst.Add((double)new Random().Next(int.MinValue, int.MaxValue));
            return lst;
        }

        private List<long> Long2Value()
        {
            List<long> lst = new List<long>();
            lst.Add(long.MaxValue);
            lst.Add(long.MinValue);
            lst.Add(0);
            lst.Add((long)new Random().Next(int.MinValue, int.MaxValue));
            return lst;
        }

        private List<ulong> ULong2Value()
        {
            List<ulong> lst = new List<ulong>();
            lst.Add(ulong.MaxValue);
            lst.Add(ulong.MinValue);
            lst.Add(0);
            lst.Add((ulong)new Random().Next(0, int.MaxValue));
            return lst;
        }

        private List<short> Short2Value()
        {
            List<short> lst = new List<short>();
            lst.Add(short.MaxValue);
            lst.Add(short.MinValue);
            lst.Add(0);
            lst.Add((short)new Random().Next(short.MinValue, short.MaxValue));
            return lst;
        }

        private static List<ushort> UShort2Value()
        {
            List<ushort> lst = new List<ushort>();
            lst.Add(ushort.MaxValue);
            lst.Add(ushort.MinValue);
            lst.Add(0);
            lst.Add((ushort)new Random().Next(ushort.MinValue, ushort.MaxValue));
            return lst;
        }

        private List<int> Int2Value()
        {
            List<int> lst = new List<int>();
            lst.Add(int.MaxValue);
            lst.Add(int.MinValue);
            lst.Add(0);
            lst.Add(new Random().Next(int.MinValue, int.MaxValue));
            return lst;
        }

        private List<uint> UInt2Value()
        {
            List<uint> lst = new List<uint>();
            lst.Add(uint.MaxValue);
            lst.Add(uint.MinValue);
            lst.Add(0);
            lst.Add((uint)(new Random().Next(0, int.MaxValue)));
            return lst;
        }
    }
}
/*------------------------------------------------------------------------------
 *  
 *  文件名:     ParamtersAnalysis.cs
 *  功能說明:   用于创建MemberInfo方法的参数列表
 *
 *  創建人:    it_Eric 
 *  創建時間:   10/30/2013 10:15:20 AM
 *   
 *  修改人:    
 *  修改說明:    
 * 
*-----------------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using netsystemsautotest;

namespace netsystemsautotest
{
    /// <summary>
    /// 用于创建MemberInfo方法的参数列表
    /// </summary>
    public class ParamtersAnalysis
    {
        public MemberInfo Member { get; set; }
        private int[] _arrlenght; //记录每维数组的长度的一维数组
        private int[] _arrpos; //记录要读取数据的位置
        private int _groupcount; //记录总数据长度
        private List<List<object>> _paramgrouplst; //记录参数组合

        public ParamtersAnalysis(MemberInfo member)
        {
            Member = member;

            Type[] types = AnalysisiParamterType();

            _paramgrouplst = AllParameterList(types);

            _arrlenght = ArrayLenght(_paramgrouplst);

            _arrpos = new int[_paramgrouplst.Count];

            _groupcount = GroupCount(_paramgrouplst);
        }

        /// <summary>
        /// 迭代返回方法参数数组
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object[]> ReturnParamters()
        {
            for (int j = 0; j < _groupcount; j++)
            {
                List<object> result = new List<object>();
                for (int i = 0; i < _paramgrouplst.Count; i++)
                {
                    if (_arrpos[i] < _paramgrouplst[i].Count)
                    {
                        result.Add(_paramgrouplst[i][_arrpos[i]]);
                    }
                }
                _arrpos = ChangeArrFlag(_arrpos, _arrlenght, _arrpos.Length);

                yield return result.ToArray();
            }
        }

        #region 私有方法

        /// <summary>
        /// 获取参数类型
        /// </summary>
        /// <returns></returns>
        private Type[] AnalysisiParamterType()
        {
            List<Type> lst = new List<Type>();
            if (Member.MemberType == MemberTypes.Method)
            {
                ParameterInfo[] paramters = ((MethodInfo) Member).GetParameters();
                foreach (var parameterInfo in paramters)
                {
                    lst.Add(parameterInfo.ParameterType);
                }
            }
            else if (Member.MemberType == MemberTypes.Property)
            {
                lst.Add(((PropertyInfo) Member).PropertyType);
            }
            else if (Member.MemberType == MemberTypes.Constructor)
            {
                ParameterInfo[] paramters = ((ConstructorInfo) Member).GetParameters();
                foreach (var parameterInfo in paramters)
                {
                    lst.Add(parameterInfo.ParameterType);
                }
            }

            return lst.ToArray();

        }

        /// <summary>
        /// 用数组保存每一个元素的长度
        /// </summary>
        /// <param name="paramgrouplst"></param>
        /// <returns></returns>
        private int[] ArrayLenght(List<List<object>> paramgrouplst)
        {
            int[] arrlenght = new int[paramgrouplst.Count];
            for (int i = 0; i < paramgrouplst.Count; i++)
            {
                arrlenght[i] = paramgrouplst[i].Count - 1;
            }
            return arrlenght;
        }

        /// <summary>
        /// 控制遍历的顺序
        /// </summary>
        /// <param name="arrpos"></param>
        /// <param name="arrlength"></param>
        /// <returns></returns>
        private int[] ChangeArrFlag(int[] arrpos, int[] arrlength, int indexflag)
        {
            int[] arrposvalue = arrpos;

            int index = indexflag - 1;
            if (index < 0)
            {
                return arrlength;
            }
            if (arrpos[index] < arrlength[index])
            {
                arrposvalue[index]++;
            }
            else
            {
                arrposvalue[index] = 0;
                arrposvalue = ChangeArrFlag(arrposvalue, arrlength, index);
            }
            return arrposvalue;
        }

        /// <summary>
        /// 计算组合数量
        /// </summary>
        /// <param name="paramgrouplst"></param>
        /// <returns></returns>
        private int GroupCount(List<List<object>> paramgrouplst)
        {
            int group = 1;

            for (int i = 0; i < paramgrouplst.Count; i++)
            {
                group = group*paramgrouplst[i].Count; //计算组合数量
            }
            return group;
        }

        /// <summary>
        /// 获取参数组合的二维数组
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        private List<List<object>> AllParameterList(Type[] types)
        {
            List<List<object>> paramgrouplst = new List<List<object>>();
            foreach (Type type in types)
            {
                List<object> lst = (List<object>) new Type2ValueConvert(type).Type2Value();
                paramgrouplst.Add(lst);
            }
            return paramgrouplst;
        }

        #endregion
    }
}

 

posted @ 2013-12-18 13:57  尼姑哪里跑  阅读(244)  评论(0)    收藏  举报