如果delegate目标方法的参数多于4个,不能用System.Linq.Expressions.Expression.GetActionType() 怎么办?

上谷鹤 2010-10-29 01:34:43
下面的代码是建立delegate , 用GetMethodDelegateType 获得 type
但是目标方法的参数很多,用 GetActionType 或 GetFuncType 都会报错。

我的代码:


var methodDelegate = Delegate.CreateDelegate(GetMethodDelegateType(proxyClassObj, serviceMethodName),
proxyClassObj, serviceMethodName, false);

private static Type GetMethodDelegateType(object testClass, string methodName)
{
var mi = testClass.GetType().GetMethod(methodName);
var typeArgs = mi.GetParameters().Select(p => p.ParameterType).ToList();

// builds a delegate type
if (mi.ReturnType == typeof(void))
{
return System.Linq.Expressions.Expression.GetActionType(typeArgs.ToArray());
}
typeArgs.Add(mi.ReturnType);
return System.Linq.Expressions.Expression.GetFuncType(typeArgs.ToArray());
}


我查了下文档 发现这两个方法 GetActionType 、 GetFuncType 的参数有要求,不能多于4或5个元素。
Parameters
typeArgs
Type: System.Type[]
An array of zero to four Type objects that specify the type arguments for the System.Action delegate type.

Return Value
Type: System.Type
The type of a System.Action delegate that has the specified type arguments.

Exceptions
Exception Condition
ArgumentException typeArgs contains more than four elements.

这该怎么办?
...全文
164 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
上谷鹤 2010-11-01
  • 打赏
  • 举报
回复
不晚 说说你的方法呗
wuhuabucai 2010-11-01
  • 打赏
  • 举报
回复
呵呵 来晚了
上谷鹤 2010-11-01
  • 打赏
  • 举报
回复
受到 Dynamic Language Runtime 的启发, 定制了支持多参数的 Action 和 Function

问题解决了。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧