源起:
经常使用编写单元测试,经常更改函数参数的传值,通过ParamsBuild配合配置文件配置就可以做到函数参数值可配置。
方便缩写的命名空间导入
1
using
System;
2 using System.Xml.Serialization;
3 using System.Text.RegularExpressions;
4 using System.Collections.Generic;
5 using System.Diagnostics;
6 using System.Reflection;
7 using System.Xml;
第一部分:参数构建器(主题设计)
2 using System.Xml.Serialization;
3 using System.Text.RegularExpressions;
4 using System.Collections.Generic;
5 using System.Diagnostics;
6 using System.Reflection;
7 using System.Xml;
代码ParamsBuild
///
<summary>
/// 参数构建器
/// </summary>
[Serializable]
public class ParamsBuild
{
/// <summary>
/// 获取或设置方法参数配置集合
/// </summary>
/// <value> The param config. </value>
[XmlElement(ElementName = " Config " )]
public MethodParams[] ParamConfig { get ; set ; }
/// <summary>
/// 获取指定类型名称的方法参数
/// </summary>
/// <param name="typeFullName"> 类型全称 </param>
/// <param name="methodName"> 方法名称 </param>
/// <returns></returns>
public MethodParams GetParamConfig( string typeFullName, string methodName)
{
MethodParams result = null ;
foreach (MethodParams pm in ParamConfig)
{
if (pm.TypeFullName == typeFullName && pm.MethodName == methodName)
{
result = pm;
break ;
}
}
return result;
}
/// <summary>
/// 获取当前调用方法的方法参数配置
/// </summary>
/// <returns></returns>
public MethodParams GetParamConfig()
{
MethodBase method = new System.Diagnostics.StackFrame( 1 ).GetMethod();
return GetParamConfig(method.DeclaringType.FullName, method.Name);
}
/// <summary>
/// 获取同类型中其他方法名的参数配置
/// </summary>
/// <param name="methodName"> 方法名称 </param>
/// <returns></returns>
public MethodParams GetParamConfig( string methodName)
{
MethodBase method = new System.Diagnostics.StackFrame( 1 ).GetMethod();
return GetParamConfig(method.DeclaringType.FullName, methodName);
}
private static ParamsBuild _instance;
/// <summary>
/// 获取配置文件中的实例
/// </summary>
public static ParamsBuild ConfigInstance
{
get
{
if (_instance == null )
{
_instance = XmlSerializeSectionHandler.GetObject < ParamsBuild > ( " ParamsBuild " );
}
return _instance;
}
}
}
/// 参数构建器
/// </summary>
[Serializable]
public class ParamsBuild
{
/// <summary>
/// 获取或设置方法参数配置集合
/// </summary>
/// <value> The param config. </value>
[XmlElement(ElementName = " Config " )]
public MethodParams[] ParamConfig { get ; set ; }
/// <summary>
/// 获取指定类型名称的方法参数
/// </summary>
/// <param name="typeFullName"> 类型全称 </param>
/// <param name="methodName"> 方法名称 </param>
/// <returns></returns>
public MethodParams GetParamConfig( string typeFullName, string methodName)
{
MethodParams result = null ;
foreach (MethodParams pm in ParamConfig)
{
if (pm.TypeFullName == typeFullName && pm.MethodName == methodName)
{
result = pm;
break ;
}
}
return result;
}
/// <summary>
/// 获取当前调用方法的方法参数配置
/// </summary>
/// <returns></returns>
public MethodParams GetParamConfig()
{
MethodBase method = new System.Diagnostics.StackFrame( 1 ).GetMethod();
return GetParamConfig(method.DeclaringType.FullName, method.Name);
}
/// <summary>
/// 获取同类型中其他方法名的参数配置
/// </summary>
/// <param name="methodName"> 方法名称 </param>
/// <returns></returns>
public MethodParams GetParamConfig( string methodName)
{
MethodBase method = new System.Diagnostics.StackFrame( 1 ).GetMethod();
return GetParamConfig(method.DeclaringType.FullName, methodName);
}
private static ParamsBuild _instance;
/// <summary>
/// 获取配置文件中的实例
/// </summary>
public static ParamsBuild ConfigInstance
{
get
{
if (_instance == null )
{
_instance = XmlSerializeSectionHandler.GetObject < ParamsBuild > ( " ParamsBuild " );
}
return _instance;
}
}
}
第二部分:方法参数配置
MethodParams
///
<summary>
/// 方法参数配置
/// </summary>
[Serializable]
public class MethodParams : ICloneable
{
/// <summary>
/// 类型全称
/// </summary>
[XmlAttribute(AttributeName = " type " )]
public string TypeFullName { get ; set ; }
/// <summary>
/// 方法名
/// </summary>
[XmlAttribute(AttributeName = " method " )]
public string MethodName { get ; set ; }
/// <summary>
/// 参数集合
/// </summary>
public Param[] Params { get ; set ; }
/// <summary>
/// 获取参数集合中特定名称的参数
/// </summary>
/// <param name="paramName"> 参数名称 </param>
/// <returns></returns>
public Param GetParam( string paramName)
{
Param p = null ;
if (Params != null && Params.Length > 0 )
{
foreach (Param pi in Params)
{
if (pi.Name == paramName)
{
p = pi;
break ;
}
}
}
return p;
}
/// <summary>
/// [穷举]获取所有方法最个可能参数的的情况(各个参数可能情况的乘积)
/// </summary>
/// <returns></returns>
public MethodParams[] GetAvailableMethodParams()
{
List < MethodParams > mparmList = new List < MethodParams > ();
bool addDefaultMethodParams = false ;
List < Param > baseParams = new
/// 方法参数配置
/// </summary>
[Serializable]
public class MethodParams : ICloneable
{
/// <summary>
/// 类型全称
/// </summary>
[XmlAttribute(AttributeName = " type " )]
public string TypeFullName { get ; set ; }
/// <summary>
/// 方法名
/// </summary>
[XmlAttribute(AttributeName = " method " )]
public string MethodName { get ; set ; }
/// <summary>
/// 参数集合
/// </summary>
public Param[] Params { get ; set ; }
/// <summary>
/// 获取参数集合中特定名称的参数
/// </summary>
/// <param name="paramName"> 参数名称 </param>
/// <returns></returns>
public Param GetParam( string paramName)
{
Param p = null ;
if (Params != null && Params.Length > 0 )
{
foreach (Param pi in Params)
{
if (pi.Name == paramName)
{
p = pi;
break ;
}
}
}
return p;
}
/// <summary>
/// [穷举]获取所有方法最个可能参数的的情况(各个参数可能情况的乘积)
/// </summary>
/// <returns></returns>
public MethodParams[] GetAvailableMethodParams()
{
List < MethodParams > mparmList = new List < MethodParams > ();
bool addDefaultMethodParams = false ;
List < Param > baseParams = new