/*------------------------------------------------------------------------------ * * 版权所有。 * * 文件名: 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{ /// /// 用于创建MemberInfo成员的方法参数列表 /// public class ParamtersAnalysis:IParamter { public MemberInfo Member { get; set; } private int[] _arrlenght; //记录每维数组的长度的一维数组 private int[] _arrpos; //记录要读取数据的位置 private int _groupcount; //记录总数据长度 private List
> _paramgrouplst; //记录参数组合 public ParamtersAnalysis(MemberInfo member) { Member = member; Type[] types = AnalysisiParamterType(); _paramgrouplst = AllParameterList(types); _arrlenght = ArrayLenght(_paramgrouplst); _arrpos = new int[_paramgrouplst.Count]; _groupcount = GroupCount(_paramgrouplst); } /// /// 迭代返回方法参数数组 /// ///
public IEnumerable
ReturnParamters() { for (int j = 0; j < _groupcount; j++) { List
result = new List(); 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 私有方法 /// /// 获取参数类型 /// ///
private Type[] AnalysisiParamterType() { List
lst = new List
(); 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(); }/* ///
/// 判断两个数组是否是相同的数组 /// ///
///
///
private bool IsSameArr(int[] arrpos,int[] arrlenght) { for (int i = 0; i < arrpos.Length; i++) { if (arrpos[i]!=arrlenght[i]) { return false; } } return true; }*/ ///
/// 用数组保存每一个元素的长度 /// ///
///
private int[] ArrayLenght(List
> paramgrouplst) { int[] arrlenght = new int[paramgrouplst.Count]; for (int i = 0; i < paramgrouplst.Count; i++) { arrlenght[i] = paramgrouplst[i].Count-1; } return arrlenght; } /// /// 控制遍历的顺序 /// /// /// ///
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; } /// /// 计算组合数量 /// /// ///
private int GroupCount(List
> paramgrouplst) { int group = 1; for (int i = 0; i < paramgrouplst.Count; i++) { group = group*paramgrouplst[i].Count; //计算组合数量 } return group; } /// /// 获取参数组合的二维数组 /// /// ///
private List
> AllParameterList(Type[] types) { List
> paramgrouplst=new List
>(); foreach (Type type in types) { List lst = (List) Type2Value(type); paramgrouplst.Add(lst); } return paramgrouplst; } /// /// 传入对应的类型返回默认值 /// /// ///
private object Type2Value(Type type) { if (type == typeof(int) || type == typeof(uint)) { return list2object
(Int2Value()); } else if (type == typeof(short) || type == typeof(ushort)) { return list2object
(Short2Value()); } else if (type == typeof(long) || type == typeof(ulong)) { return list2object
(Long2Value()); } else if (type == typeof(double)) { return list2object
(Double2Value()); } else if (type == typeof(float)) { return list2object
(Float2Value()); } else if (type == typeof(string)) { return list2object
(String2Value()); } else if (type == typeof(bool)) { return list2object
(Bool2Value()); } else if (type == typeof(char)) { return list2object
(Char2Value()); } else if (type == typeof(byte)) { return list2object
(Byte2Value()); } else if (type == typeof(DateTime)) { return list2object
(Datetimes2Value()); } else { return null; } } private static List
list2object
(List
lst ) { List
result=new List(); foreach (T item in lst) { result.Add(item); } return result; } private static List
Datetimes2Value() { List
lst = new List
(); lst.Add(DateTime.Now); lst.Add(DateTime.Today); lst.Add(DateTime.MaxValue); lst.Add(DateTime.MinValue); return lst; } private static List
Byte2Value() { List
lst = new List
(); lst.Add(byte.MaxValue); lst.Add(byte.MinValue); lst.Add(default(byte)); return lst; } private static List
Char2Value() { List
lst = new List
(); lst.Add((char)new Random().Next(33, 126)); return lst; } private static List
Bool2Value() { List
lst = new List
(); lst.Add(true); lst.Add(false); return lst; } private static List
String2Value() { List
lst = new List
(); lst.Add(string.Empty); lst.Add(null); lst.Add("abcdefghijklmnopqrstuvwxyz"); lst.Add("abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc" + "defghijklmnopqrstuvwxyzzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); return lst; } private static List
Float2Value() { List
lst = new List
(); 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
Double2Value() { List
lst = new List
(); 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
Long2Value() { List
lst = new List
(); 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
Short2Value() { List
lst = new List
(); 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
Int2Value() { List
lst = new List
(); 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 { /// /// The main entry point for the application. /// [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); List
typelst=new List
(); 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
paramters = pa.ReturnParamters(); List
paramterlst = new List
(); 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; } }}