add supports for InvokeGeneric methods to return Types supports ...

This commit is contained in:
2026-05-24 22:48:12 +03:30
parent 39452b51ff
commit f40f9eeac0
+60 -4
View File
@@ -21,6 +21,30 @@ namespace xCommons.Helpers
Type runtimeType, Type runtimeType,
params object[] args params object[] args
) )
{
//
source.InvokeGenericMethod<object>(
args: args,
methodName: methodName,
runtimeType: runtimeType
);
}
/// <summary>
/// Invoke a Generic Method of Specified Type ...
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="source"></param>
/// <param name="methodName"></param>
/// <param name="runtimeType"></param>
/// <param name="args"></param>
/// <returns></returns>
public static TResponse InvokeGenericMethod<TResponse>(
this Type source,
string methodName,
Type runtimeType,
params object[] args
)
{ {
// //
var overLoads = source.GetGenericMethodInfo( var overLoads = source.GetGenericMethodInfo(
@@ -28,7 +52,7 @@ namespace xCommons.Helpers
); );
if (!overLoads.HasChild()) if (!overLoads.HasChild())
{ {
return; return default;
} }
// //
@@ -36,7 +60,11 @@ namespace xCommons.Helpers
MethodInfo genericMethod = method.MakeGenericMethod(runtimeType); MethodInfo genericMethod = method.MakeGenericMethod(runtimeType);
// //
genericMethod.Invoke(null, args); var resultObj = genericMethod.Invoke(null, args);
var result = (TResponse)resultObj;
//
return result;
} }
/// <summary> /// <summary>
@@ -52,6 +80,30 @@ namespace xCommons.Helpers
Type[] runtimeTypes, Type[] runtimeTypes,
params object[] args params object[] args
) )
{
//
source.InvokeGenericMethod<object>(
args: args,
methodName: methodName,
runtimeTypes: runtimeTypes
);
}
/// <summary>
/// Invoke a Generic Method of Specified Type ...
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="source"></param>
/// <param name="methodName"></param>
/// <param name="runtimeTypes"></param>
/// <param name="args"></param>
/// <returns></returns>
public static TResponse InvokeGenericMethod<TResponse>(
this Type source,
string methodName,
Type[] runtimeTypes,
params object[] args
)
{ {
// //
var overLoads = source.GetGenericMethodInfo( var overLoads = source.GetGenericMethodInfo(
@@ -59,7 +111,7 @@ namespace xCommons.Helpers
); );
if (!overLoads.HasChild()) if (!overLoads.HasChild())
{ {
return; return default;
} }
// //
@@ -69,7 +121,11 @@ namespace xCommons.Helpers
MethodInfo genericMethod = method.MakeGenericMethod(runtimeTypes); MethodInfo genericMethod = method.MakeGenericMethod(runtimeTypes);
// //
genericMethod.Invoke(null, args); var resultObj = genericMethod.Invoke(null, args);
var result = resultObj.ConvertTo<TResponse>();
//
return result;
} }
/// <summary> /// <summary>