using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using xCommons.Extensions;
namespace xCommons.Helpers
{
public static class XGenericHelper
{
///
/// Invoke a Generic Method of Specified Type ...
///
///
///
///
///
public static void InvokeGenericMethod(
this Type source,
string methodName,
Type runtimeType,
params object[] args
)
{
//
var overLoads = source.GetGenericMethodInfo(
methodName: methodName
);
if (!overLoads.HasChild())
{
return;
}
//
var method = overLoads.ElementAt(0);
MethodInfo genericMethod = method.MakeGenericMethod(runtimeType);
//
genericMethod.Invoke(null, args);
}
///
/// Extract a Generic Method's Overloads ...
///
///
///
///
public static List GetGenericMethodInfo(
this Type source,
string methodName
)
{
//
var methods = source
.GetMethods();
//
var result = methods
.Where(mi =>
mi.Name == methodName &&
mi.IsGenericMethodDefinition)
.ToList();
//
return result;
}
}
}