zoukankan      html  css  js  c++  java
  • 参数测试

    /*------------------------------------------------------------------------------
     *  
     *  版权所有。
     *  
     *  文件名:     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;
    
    namespace NetAutoTest
    {
        /// <summary>
        /// 用于创建MemberInfo成员的方法参数列表
      /// </summary>
        public class ParamtersAnalysis:IParamter
        {
            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="arrpos"></param>
            /// <param name="arrlenght"></param>
            /// <returns></returns>
            private  bool IsSameArr(int[] arrpos,int[] arrlenght)
            {
                for (int i = 0; i < arrpos.Length; i++)
                {
                    if (arrpos[i]!=arrlenght[i])
                    {
                        return false;
                    }
                }
                return true;
            }
    */
    
            /// <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>) Type2Value(type);
                   paramgrouplst.Add(lst);
               }
               return paramgrouplst;
           }
    
           /// <summary>
            /// 传入对应的类型返回默认值
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            private object Type2Value(Type type)
            {
               
                if (type == typeof(int) || type == typeof(uint))
                {
                       return  list2object<int>(Int2Value());
                }
                else if (type == typeof(short) || type == typeof(ushort))
                {
                    return list2object<short>(Short2Value());
                }
                else if (type == typeof(long) || type == typeof(ulong))
                {
                    return list2object<long>(Long2Value());
                    
                }
                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 null;
                }
            }
    
          
    
           private static List<object> list2object<T>(List<T> lst )
           {
               List<object> result=new List<object>();
               foreach (T item in lst)
               {
                   result.Add(item);
               }
               return result;
           }
    
    
           private static 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 static List<byte> Byte2Value()
            {
                List<byte> lst = new List<byte>();
                lst.Add(byte.MaxValue);
                lst.Add(byte.MinValue);
                lst.Add(default(byte));
                return lst;
            }
    
            private static List<char> Char2Value()
            {
                List<char> lst = new List<char>();
                lst.Add((char)new Random().Next(33, 126));
                return lst;
            }
    
            private static List<bool> Bool2Value()
            {
                List<bool> lst = new List<bool>();
                lst.Add(true);
                lst.Add(false);
                return lst;
            }
    
            private static 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 static 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 static 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 static 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 static 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<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;
            }
            #endregion
        }
    
    }


    一个排列组合的案例,将一维数组中每一维上的数据进行组合,得到所有的组合,放入paramgrouplst中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using NetAutoTest;
    using System.Reflection;
    
    namespace ReflectTestDemo
    {
        internal static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            private static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                List<Type> typelst=new List<Type>();
                typelst.Add(typeof(string));
                typelst.Add(typeof(bool));
                typelst.Add(typeof(int));
                MemberInfo member = typeof(Person).GetConstructor(typelst.ToArray());
                // MemberInfo member = typeof (Person).GetMethod("Test");
                ParamtersAnalysis pa = new ParamtersAnalysis(member);
                IEnumerable<object[]> paramters = pa.ReturnParamters();
                List<object[]> paramterlst = new List<object[]>();
                foreach (object[] paramter in paramters)
                {
                    paramterlst.Add(paramter);
                }
    
            }
    
          
    
            public static string TestProperty { get; set; }
    
            public  static void Test(string name,bool flag)
            {
                return;
            }
        }
    
        public class Person
        {
            public Person(string name,bool bol,int a)
            {
    
            }
            public Person(int  age)
            {
    
            }
    
            public string Name { get; set; }
            public int Sex { get; set; }
    
            public static void Test(string name,bool flag,int a)
            {
                return;
            }
        }
    }
  • 相关阅读:
    实例教学在MySQL中若何导出整个数据库
    在Linux下Turbomail简易快捷的装置方式
    Fedora下编译mitscheme
    Fedora 9可以不敌RedHat 9光辉
    实用伎俩:Ubuntu Linux 8.04设置与优化
    Linux下给WordPress建设伪静态
    红旗桌面版本最新使用要领和题目问题解答100例5
    知识管理系统红旗Linux/KM.Center
    python 虚拟环境的安装
    python 闭包
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3404021.html
Copyright © 2011-2022 走看看