如果我在字符串中有函数名称,如何调用函数 [英] How To Call A Function , If I Have Function Name In String

查看:162
本文介绍了如果我在字符串中有函数名称,如何调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数名列表,例如do_work(),do_work1(),...

提示用户进行输入后,我的计算将生成一个字符串,该字符串是我已经实现的函数名称.

我正在以字符串形式获取函数名称,现在我不知道如何调用该函数

谁能帮我...

I have a list of function names like do_work(),do_work1(),...

After prompting to user to take inputs my calculations generates a string that string is a function name which i have already implemented.

I am getting function name as a string, now I don''t know how to call that function

Can anybody help me...

推荐答案

它需要特定于脚本语言的技巧".对于Python,这是函数eval():
https://docs.python.org/2/library/functions.html#eval [ ^ ].

另请参见:
http://en.wikipedia.org/wiki/Eval [ http://en.wikipedia.org/wiki/Eval#Theory [ http://lybniz2.sourceforge.net/safeeval.html [
It requires a "trick" specific to scripting languages. In case of Python, this is the function eval():
https://docs.python.org/2/library/functions.html#eval[^].

See also:
http://en.wikipedia.org/wiki/Eval[^],
http://en.wikipedia.org/wiki/Eval#Theory[^].

Use it with care. Look at this article: http://lybniz2.sourceforge.net/safeeval.html[^].

—SA


kishore问:

是否可以使用其他语言,例如C,Java吗?

Is it possible in another languages like C, Java?

有趣的问题.
基本上没有由于明显的原因,用编译器语言实现这种事情通常是不可能的:您可能要使用的函数必须是编译器及其调用.

但是,在某些技术中,类似的事情是可能的,但是从技术上讲,这与编译有关是完全不同的事情.

例如,.NET在运行时支持编译,并且该框架保证了某些编译器的存在.但是解决方案在概念上有很大的不同:您将具有完整项目所有属性的代码编译到一个程序集中,然后加载该程序集,找到实现的接口并使用反射实例化一些实现类,然后使用该实例执行一些代码,即使是与您最初使用的编译过程相同的过程.但是问题只是从这里开始:如果您要一遍又一遍地执行(在同一过程中),则会发生内存泄漏,因为不可能卸载已加载的程序集.解决方案是在同一过程中拥有一个单独的应用程序域,然后每次创建该应用程序域并删除整个域.但是随后您必须遍历域边界,这并不容易,因为域是隔离的,因此您将需要使用IPC.这样,您可以为任何对象创建一个编译计算器".这是一个非常有趣的东西,我已经做了所有,甚至更多.但是,正如您所看到的,这与解释语言的方法非常不同.

—SA

Interesting question.
Basically, no. Implementation of such thing in compiler languages is generally impossible, by apparent reasons: the function you may want to use needs to be compilers, as well as its call.

However, in certain technologies, something analogous is possible, but, technically, this is a very different thing, related to compilation.

For example, .NET supports compilation during runtime, and the framework guarantees the presence of some compilers. But the solution is conceptually very different: you compile some code with all the attributes of complete project into an assembly, then load this assembly, finds the implemented interface and instantiate some implementing class using reflection, and than use this instance to execute some code, even in the same process which you used in the compilation in first place. But the problems are only started here: if you want to do this over and over (in the same process), you will have a memory leak, because it''s impossible to unload a loaded assembly. The solution is to have a separate application domain in the same process, and each time create it and delete the whole domain. But then you have to work through the domain boundary, which is not so easy, because the domains are isolated, so you would need to use IPC. This way, you can create a "compiling calculator" for anything. This is a very interesting stuff, I''ve done all that, and more. But, as you can see, this is very, very different from the approach of the interpretive languages.

—SA


实际上,您可以使用c#中的反射来归档它.像这样的东西:

In fact,you can archieve this using reflection in c#. Something like:

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheMethodString);
theMethod.Invoke(this, userParameters);



但是您必须照顾SA在他的回答中解释得很好的内容



But you have to take care of what SA explained very well in his answer


这篇关于如果我在字符串中有函数名称,如何调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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