本文整理汇总了C#中IMethodInfo.MakeGenericMethodEx方法的典型用法代码示例。如果您正苦于以下问题:C# IMethodInfo.MakeGenericMethodEx方法的具体用法?C# IMethodInfo.MakeGenericMethodEx怎么用?C# IMethodInfo.MakeGenericMethodEx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMethodInfo
的用法示例。
在下文中一共展示了IMethodInfo.MakeGenericMethodEx方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildFrom
/// <summary>
/// Construct one or more TestMethods from a given MethodInfo,
/// using available parameter data.
/// </summary>
/// <param name="method">The MethodInfo for which tests are to be constructed.</param>
/// <param name="suite">The suite to which the tests will be added.</param>
/// <returns>One or more TestMethods</returns>
public IEnumerable<TestMethod> BuildFrom(IMethodInfo method, Test suite)
{
List<TestMethod> tests = new List<TestMethod>();
#if NETCF
if (method.ContainsGenericParameters)
{
var genericParams = method.GetGenericArguments();
var numGenericParams = genericParams.Length;
var o = new object();
var tryArgs = Enumerable.Repeat(o, numGenericParams).ToArray();
IMethodInfo mi;
try
{
// This fails if the generic method has constraints
// that are not met by object.
mi = method.MakeGenericMethodEx(tryArgs);
if (mi == null)
return tests;
}
catch
{
return tests;
}
var par = mi.GetParameters();
if (par.Length == 0)
return tests;
var sourceData = par.Select(p => _dataProvider.GetDataFor(p)).ToArray();
foreach (var parms in _strategy.GetTestCases(sourceData))
{
mi = method.MakeGenericMethodEx(parms.Arguments);
if (mi == null)
{
var tm = new TestMethod(method, suite);
tm.RunState = RunState.NotRunnable;
tm.Properties.Set(PropertyNames.SkipReason, "Incompatible arguments");
tests.Add(tm);
}
else
tests.Add(_builder.BuildTestMethod(mi, suite, (TestCaseParameters)parms));
}
return tests;
}
#endif
IParameterInfo[] parameters = method.GetParameters();
if (parameters.Length > 0)
{
IEnumerable[] sources = new IEnumerable[parameters.Length];
try
{
for (int i = 0; i < parameters.Length; i++)
sources[i] = _dataProvider.GetDataFor(parameters[i]);
}
catch (InvalidDataSourceException ex)
{
var parms = new TestCaseParameters();
parms.RunState = RunState.NotRunnable;
parms.Properties.Set(PropertyNames.SkipReason, ex.Message);
tests.Add(_builder.BuildTestMethod(method, suite, parms));
return tests;
}
foreach (var parms in _strategy.GetTestCases(sources))
tests.Add(_builder.BuildTestMethod(method, suite, (TestCaseParameters)parms));
}
return tests;
}
示例2: GetParametersForTestCase
private TestCaseParameters GetParametersForTestCase(IMethodInfo method)
{
TestCaseParameters parms;
try
{
#if NETCF
var tmethod = method.MakeGenericMethodEx(Arguments);
if (tmethod == null)
throw new NotSupportedException("Cannot determine generic types from probing");
method = tmethod;
#endif
IParameterInfo[] parameters = method.GetParameters();
int argsNeeded = parameters.Length;
int argsProvided = Arguments.Length;
parms = new TestCaseParameters(this);
// Special handling for params arguments
if (argsNeeded > 0 && argsProvided >= argsNeeded - 1)
{
IParameterInfo lastParameter = parameters[argsNeeded - 1];
Type lastParameterType = lastParameter.ParameterType;
Type elementType = lastParameterType.GetElementType();
if (lastParameterType.IsArray && lastParameter.IsDefined<ParamArrayAttribute>(false))
{
if (argsProvided == argsNeeded)
{
Type lastArgumentType = parms.Arguments[argsProvided - 1].GetType();
if (!lastParameterType.IsAssignableFrom(lastArgumentType))
{
Array array = Array.CreateInstance(elementType, 1);
array.SetValue(parms.Arguments[argsProvided - 1], 0);
parms.Arguments[argsProvided - 1] = array;
}
}
else
{
object[] newArglist = new object[argsNeeded];
for (int i = 0; i < argsNeeded && i < argsProvided; i++)
newArglist[i] = parms.Arguments[i];
int length = argsProvided - argsNeeded + 1;
Array array = Array.CreateInstance(elementType, length);
for (int i = 0; i < length; i++)
array.SetValue(parms.Arguments[argsNeeded + i - 1], i);
newArglist[argsNeeded - 1] = array;
parms.Arguments = newArglist;
argsProvided = argsNeeded;
}
}
}
//if (method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
// parms.Arguments = new object[]{parms.Arguments};
// Special handling when sole argument is an object[]
if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
{
if (argsProvided > 1 ||
argsProvided == 1 && parms.Arguments[0].GetType() != typeof(object[]))
{
parms.Arguments = new object[] { parms.Arguments };
}
}
if (argsProvided == argsNeeded)
PerformSpecialConversions(parms.Arguments, parameters);
}
catch (Exception ex)
{
parms = new TestCaseParameters(ex);
}
return parms;
}
示例3: GetTestCasesFor
/// <summary>
/// Returns a set of ITestCaseDataItems for use as arguments
/// to a parameterized test method.
/// </summary>
/// <param name="method">The method for which data is needed.</param>
/// <returns></returns>
private IEnumerable<ITestCaseData> GetTestCasesFor(IMethodInfo method)
{
List<ITestCaseData> data = new List<ITestCaseData>();
try
{
IEnumerable source = GetTestCaseSource(method);
if (source != null)
{
#if NETCF
int numParameters = method.IsGenericMethodDefinition ? 0 : method.GetParameters().Length;
#else
int numParameters = method.GetParameters().Length;
#endif
foreach (object item in source)
{
var parms = item as ITestCaseData;
if (parms == null)
{
object[] args = item as object[];
if (args != null)
{
#if NETCF
if (method.IsGenericMethodDefinition)
{
var mi = method.MakeGenericMethodEx(args);
numParameters = mi == null ? 0 : mi.GetParameters().Length;
}
#endif
if (args.Length != numParameters)//parameters.Length)
args = new object[] { item };
}
else if (item is Array)
{
Array array = item as Array;
#if NETCF
if (array.Rank == 1 && (method.IsGenericMethodDefinition || array.Length == numParameters))//parameters.Length))
#else
if (array.Rank == 1 && array.Length == numParameters)//parameters.Length)
#endif
{
args = new object[array.Length];
for (int i = 0; i < array.Length; i++)
args[i] = array.GetValue(i);
#if NETCF
if (method.IsGenericMethodDefinition)
{
var mi = method.MakeGenericMethodEx(args);
if (mi == null || array.Length != mi.GetParameters().Length)
args = new object[] {item};
}
#endif
}
else
{
args = new object[] { item };
}
}
else
{
args = new object[] { item };
}
parms = new TestCaseParameters(args);
}
if (this.Category != null)
foreach (string cat in this.Category.Split(new char[] { ',' }))
parms.Properties.Add(PropertyNames.Category, cat);
data.Add(parms);
}
}
}
catch (Exception ex)
{
data.Clear();
data.Add(new TestCaseParameters(ex));
}
return data;
}
示例4: GetTestCasesFor
/// <summary>
/// Returns a set of ITestCaseDataItems for use as arguments
/// to a parameterized test method.
/// </summary>
/// <param name="method">The method for which data is needed.</param>
/// <returns></returns>
private IEnumerable<ITestCaseData> GetTestCasesFor(IMethodInfo method)
{
List<ITestCaseData> data = new List<ITestCaseData>();
try
{
IEnumerable source = GetTestCaseSource(method);
if (source != null)
{
foreach (object item in source)
{
// First handle two easy cases:
// 1. Source is null. This is really an error but if we
// throw an exception we simply get an invalid fixture
// without good info as to what caused it. Passing a
// single null argument will cause an error to be
// reported at the test level, in most cases.
// 2. User provided an ITestCaseData and we just use it.
ITestCaseData parms = item == null
? new TestCaseParameters(new object[] { null })
: item as ITestCaseData;
if (parms == null)
{
// 3. An array was passed, it may be an object[]
// or possibly some other kind of array, which
// TestCaseSource can accept.
var args = item as object[];
if (args == null && item is Array)
{
Array array = item as Array;
#if NETCF
bool netcfOpenType = method.IsGenericMethodDefinition;
#else
bool netcfOpenType = false;
#endif
int numParameters = netcfOpenType ? array.Length : method.GetParameters().Length;
if (array != null && array.Rank == 1 && array.Length == numParameters)
{
// Array is something like int[] - convert it to
// an object[] for use as the argument array.
args = new object[array.Length];
for (int i = 0; i < array.Length; i++)
args[i] = array.GetValue(i);
}
}
// Check again if we have an object[]
if (args != null)
{
#if NETCF
if (method.IsGenericMethodDefinition)
{
var mi = method.MakeGenericMethodEx(args);
if (mi == null)
throw new NotSupportedException("Cannot determine generic Type");
method = mi;
}
#endif
var parameters = method.GetParameters();
var argsNeeded = parameters.Length;
var argsProvided = args.Length;
// If only one argument is needed, our array may actually
// be the bare argument. If it is, we should wrap it in
// an outer object[] representing the list of arguments.
if (argsNeeded == 1)
{
var singleParmType = parameters[0].ParameterType;
if (argsProvided == 0 || typeof(object[]).IsAssignableFrom(singleParmType))
{
if (argsProvided > 1 || singleParmType.IsAssignableFrom(args.GetType()))
{
args = new object[] { item };
}
}
}
}
else // It may be a scalar or a multi-dimensioned array. Wrap it in object[]
{
args = new object[] { item };
}
parms = new TestCaseParameters(args);
}
if (this.Category != null)
foreach (string cat in this.Category.Split(new char[] { ',' }))
parms.Properties.Add(PropertyNames.Category, cat);
data.Add(parms);
//.........这里部分代码省略.........
示例5: GetParametersForTestCase
private TestCaseParameters GetParametersForTestCase(IMethodInfo method)
{
TestCaseParameters parms;
try
{
#if NETCF
var tmethod = method.MakeGenericMethodEx(Arguments);
if (tmethod == null)
throw new NotSupportedException("Cannot determine generic types from probing");
method = tmethod;
#endif
IParameterInfo[] parameters = method.GetParameters();
int argsNeeded = parameters.Length;
int argsProvided = Arguments.Length;
parms = new TestCaseParameters(this);
parms.Arguments = new object[] { 1, 1, 1 };
if (argsProvided == argsNeeded)
PerformSpecialConversions(parms.Arguments, parameters);
}
catch (Exception ex)
{
parms = new TestCaseParameters(ex);
}
return parms;
}