如何实现动态调用函数,即使用c#根据数据库值确定要调用的函数的正确性 [英] how to achieve calling a function dynamically, thats right the function to be called is decided from database values, using c#

查看:97
本文介绍了如何实现动态调用函数,即使用c#根据数据库值确定要调用的函数的正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序非常大,因此提供了简短的背景知识和问题

the application is very large so giving a brief back ground and the problem

当用户登录时,将显示一个按钮,其中包含他所使用的功能的文本

when the user logs in, a button is displayed having the text of the function he is allowed to call.

被允许的功能被映射到数据库表中

the function he is allowed is mapped in the database table

它确保了实际函数的名称与数据库中使用的名称相同。

its made sure that the name of the actual function is same to the ones used in the db.

提取名称,并存储为按钮的文本字段以及字符串变量。

the name is extracted, and stored as text field of button and also in a string variable.

现在我应该如何使用名称存储在其中的字符串变量来调用此函数!

now how am i supposed to call this function using the string variable which has the name stored in it!

就像我们键入

功能名称();

但是在这里我不知道名字,运行时的字符串确实使我不能像这样

but here i dont know the name, the string at run time does so i cant write like

string()!!

string()!!?

推荐答案

您将需要使用反射来做到这一点。这是您需要执行的操作的粗略草图:

You will need to use reflection to do this. Here is a rough sketch of what you need to do:

// Get the Type on which your method resides:
Type t = typeof(SomeType);
// Get the method
MethodInfo m = t.GetMethod("methodNameFromDb");
// Invoke dynamically
m.Invoke(instance, null);

根据您的实际需求,您将不得不对此稍作修改-在上查找使用的方法和类型MSDN: MethodInfo 调用

Depending on your actual needs you will have to modify this a little - lookup the used methods and types on MSDN: MethodInfo, Invoke

这篇关于如何实现动态调用函数,即使用c#根据数据库值确定要调用的函数的正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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