如何动态定义函数名称并调用这些函数? [英] How to dynamically define function name and call these functions?

查看:115
本文介绍了如何动态定义函数名称并调用这些函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义10个这样的函数:
SetData1()
SetData2()
SetData3()
SetData4()
SetData5()
SetData6()
SetData7()
SetData8()
SetData9()
SetData10()

如何在运行时动态定义函数,而不是对其进行硬接线?

就像Zoltan所说的,运行时编译可以让您做到这一点,但是我也怀疑如果您需要在运行时进行编译,这可能是一个主要的设计问题.定期进行.

为什么您认为需要这个?您是否需要在运行时声明函数主体?

我可能会考虑尝试使用委托而不是运行时编译.如果可以的话,那将是维护(和调试!)的简单得多的解决方案.


您应该可以使用Roslyn.以下内容提供了有关使用Roslyn的一些信息:使用Roslyn ScriptEngine进行ValueConverter处理用户输入 [ ^ ].由于我不知道详细信息,因此无法知道是否有帮助.


I need to define 10 functions like this:
SetData1()
SetData2()
SetData3()
SetData4()
SetData5()
SetData6()
SetData7()
SetData8()
SetData9()
SetData10()

How can I define the function dynamically in run-time instead of hardwiring it?

Thanks

解决方案

As Zoltan says, runtime compilation will let you do this, but I also suspect that this is probably a major design problem if you need to do this on a regular basis.

Why do you think you need this? Do you need to declare the function body at runtime?

I would probably look at trying to use delegates instead of runtime compilation. If you can, that would be a much, much easier solution to maintain (and debug!)


You should be able to use Roslyn. The following has some information on using Roslyn for this: Using Roslyn ScriptEngine for a ValueConverter to process user input[^]. Since I do not know details, cannot know if this will help.


这篇关于如何动态定义函数名称并调用这些函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆