如果参数数组出现null,则无法精确匹配,可按参数个数进行匹配nnhy authored at 2015-03-27 16:31:44
diff --git a/NewLife.Core/Reflection/EmitReflect.cs b/NewLife.Core/Reflection/EmitReflect.cs
index 4f0bcb2..53008db 100644
--- a/NewLife.Core/Reflection/EmitReflect.cs
+++ b/NewLife.Core/Reflection/EmitReflect.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
@@ -27,8 +28,35 @@ namespace NewLife.Reflection
/// <returns></returns>
public override MethodInfo GetMethod(Type type, String name, params Type[] paramTypes)
{
- var method = base.GetMethod(type, name, paramTypes);
- if (method != null) return method;
+ // 参数数组必须为空,或者所有参数类型都不为null,才能精确匹配
+ if (paramTypes == null || paramTypes.Length == 0 || paramTypes.All(t => t != null))
+ {
+ var method = base.GetMethod(type, name, paramTypes);
+ if (method != null) return method;
+ }
+
+ // 任意参数类型为null,换一种匹配方式
+ if (paramTypes.Any(t => t == null))
+ {
+ var ms = GetMethods(type, name, paramTypes.Length);
+ if (ms == null || ms.Length == 0) return null;
+
+ // 对比参数
+ foreach (var mi in ms)
+ {
+ var ps = mi.GetParameters();
+ var flag = true;
+ for (int i = 0; i < ps.Length; i++)
+ {
+ if (paramTypes[i] != null && !ps[i].ParameterType.IsAssignableFrom(paramTypes[i]))
+ {
+ flag = false;
+ break;
+ }
+ }
+ if (flag) return mi;
+ }
+ }
return TypeX.GetMethod(type, name, paramTypes);
}
diff --git a/NewLife.Core/Reflection/IReflect.cs b/NewLife.Core/Reflection/IReflect.cs
index 0d8bd0e..4a92f65 100644
--- a/NewLife.Core/Reflection/IReflect.cs
+++ b/NewLife.Core/Reflection/IReflect.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
@@ -26,6 +27,13 @@ namespace NewLife.Reflection
/// <returns></returns>
MethodInfo GetMethod(Type type, String name, params Type[] paramTypes);
+ /// <summary>获取指定名称的方法集合,支持指定参数个数来匹配过滤</summary>
+ /// <param name="type"></param>
+ /// <param name="name"></param>
+ /// <param name="paramCount">参数个数,-1表示不过滤参数个数</param>
+ /// <returns></returns>
+ MethodInfo[] GetMethods(Type type, String name, Int32 paramCount = -1);
+
/// <summary>获取属性</summary>
/// <param name="type">类型</param>
/// <param name="name">名称</param>
@@ -182,6 +190,27 @@ namespace NewLife.Reflection
return type.GetMethod(name, bf, null, paramTypes, null);
}
+ /// <summary>获取指定名称的方法集合,支持指定参数个数来匹配过滤</summary>
+ /// <param name="type"></param>
+ /// <param name="name"></param>
+ /// <param name="paramCount">参数个数,-1表示不过滤参数个数</param>
+ /// <returns></returns>
+ public virtual MethodInfo[] GetMethods(Type type, String name, Int32 paramCount = -1)
+ {
+ var ms = type.GetMethods(bf);
+ if (ms == null || ms.Length == 0) return ms;
+
+ var list = new List<MethodInfo>();
+ foreach (var item in ms)
+ {
+ if (item.Name == name)
+ {
+ if (paramCount >= 0 && item.GetParameters().Length == paramCount) list.Add(item);
+ }
+ }
+ return list.ToArray();
+ }
+
/// <summary>获取属性</summary>
/// <param name="type">类型</param>
/// <param name="name">名称</param>
diff --git a/NewLife.Core/Reflection/Reflect.cs b/NewLife.Core/Reflection/Reflect.cs
index 05ecf01..0a4c13f 100644
--- a/NewLife.Core/Reflection/Reflect.cs
+++ b/NewLife.Core/Reflection/Reflect.cs
@@ -41,6 +41,18 @@ namespace NewLife.Reflection
return _Provider.GetMethod(type, name, paramTypes);
}
+ /// <summary>获取指定名称的方法集合,支持指定参数个数来匹配过滤</summary>
+ /// <param name="type"></param>
+ /// <param name="name"></param>
+ /// <param name="paramCount">参数个数,-1表示不过滤参数个数</param>
+ /// <returns></returns>
+ public static MethodInfo[] GetMethodsEx(this Type type, String name, Int32 paramCount = -1)
+ {
+ if (String.IsNullOrEmpty(name)) return null;
+
+ return _Provider.GetMethods(type, name, paramCount);
+ }
+
/// <summary>获取属性。搜索私有、静态、基类,优先返回大小写精确匹配成员</summary>
/// <param name="type">类型</param>
/// <param name="name">名称</param>
@@ -128,13 +140,16 @@ namespace NewLife.Reflection
{
Type t = null;
if (item != null) t = item.GetType();
+
list.Add(t);
}
+ // 如果参数数组出现null,则无法精确匹配,可按参数个数进行匹配
var method = GetMethodEx(type, name, list.ToArray());
if (method == null) return false;
value = Invoke(target, method, parameters);
+
return true;
}