让我问一个关于C#的问题

问题描述:

我在下面创建了一个函数

I have one function is created in the following

public void ExecStringToFunction(int pn)
{
    switch(pn)
    {
        case 1:
            frmform1 frm1=new frmform1();
            frm1.Showdialog();
            break;
        case 2:
            frmform2 frm2=new frmform2();
            frm2.Showdialog();
        default:
            break;
    }
}

private void butExec_Click(object sender, EventArgs e)
{
    int ni=1;
    string strExec="ExecStringToFunction("+ ni.ToString()+")";      
}



如何执行此函数的此字符串变量???

请给我发送以下电子邮件警报:[已删除电子邮件]



how to execute this function this string variable ???

Please send me email alerts for: [Email Removed]

您要执行字符串变量中的代码?不,那很不好.如果您认为需要这样做,那么您的设计就会遇到严重问题.

您为什么还要将这样的代码结构化?从您发布的内容来看,根本不需要这样做.
You want to execute the code in the string variable?? No, that''s bad. If you think you need to do that, you''ve got a serious problem with your design.

Why do you even have this code structured like this?? From what you''ve posted, there is no need to do this what-so-ever.


Dave K.在他上面的解决方案中是绝对正确的:您不这样做想要将字符串的内容作为代码执行,除非您无法避免:这对屁股很痛苦;但是,这并不意味着它无法完成.

实际上,在C#中有两种方法可以解决此问题,一种方法是使用System.CodeDom从字符串中创建/编译/运行内存中的代码,另一种方法是使用Reflection.Emit并逐步构建代码.片段,通过解析您的字符串.

此CP文章向您展示了CodeDom技术的示例:[ ^ ].另一篇较旧的CP文章:[ ^ ].另一个:[ ^ ].
Dave K., in his solution above, is absolutely correct: you don''t want to execute the contents of a string as code unless you just can''t avoid it: it''s a big pain in the butt; however, that does not mean it cannot be done.

In practice, there are two ways to approach this in C#, one through using System.CodeDom to create/compile/run code in memory from a string, and the other way is to use Reflection.Emit, and build code, piece by bloody piece, from parsing your string.

This CP article shows you an example of the CodeDom technique: [^]. Another, older, CP article: [^]. And another: [^].


您好,

作为处理此类内容的方法,您可以使用反射.

Hello,

As way to work with such stuff you just can use reflection.

<pre>// We using  that 
using System.Reflection;

int ni = 1;
// we are call local method
Type _type = this.GetType();
// look for method we interested in
MethodInfo _method = _type.GetMethod("ExecStringToFunction");
// Execute that method
if (_method != null) _method.Invoke(this, new object[] { ni });



在这里,我在本地空间中查看该函数,并使用参数执行它.
您只需要制作解析器或使用一些现有的解析器就可以解析函数名称和参数.啊,别忘了abt权限.

对于所有想知道该功能有什么必要的辩论者,而这并非设计或其他目的.例如:我做了一个巨大的库,可以用在运行时编译的运行时脚本进行扩展.因此,我使用C#或VB.NET作为扩展库功能的脚本.示例3:我手动加载程序集,并在运行时检查方法而不是作为参考程序集-这是.NET插件开发的方法(也可以调用托管exe方法);例3:拥有具有巨大结构且具有可变数量的属性的类,并且可以将任意数量的子结构作为属性值-在这里最好使用反射来提取属性名称作为类型并基于其调用方法.

另外,对于辩论者来说,问题是正确的,为什么您要从那个地方进行讨论?
如果您不知道如何制作,请不要发布解决方案.或者您只是通过发布没有任何信息的答案或发表您的问题不正确"之类的评论来赢得声誉?也许您只是开始阅读书籍以为他人提供良好的答案?

问候,
Maxim.



Here I look the function in local space and execute it with the parameter.
You just need to make the parser or use some of existing one which will parse function name and arguments. Ah also don''t forget abt permissions.

For all debators who wonder what for that functionality can be necessary, and it not design or some other things. For example: I did huge library which can be extended with runtime scripts which were compiled at runtime. So I use C# or VB.NET as a script for extending library functionality. Example 3: I load assembly manually and check for method at runtime not as a referenced assembly - this for .NET plugins development (this way also you can call a managed exe methods); Example 3: have class with the huge structure with a variable number of properties and can be any number of substructures as property value - here is also better to use reflection to extract property name as type and call the method based on it.

Also, for debators, the question was asked correctly, why are you making discussion from that place?
If you don''t know how to make it just do not post the solution. Or you just gaining your reputation via posting answers without any information or making comments that "your question is not correct" or something like that? Maybe you just start reading the books to give the good answers for others?

Regards,
Maxim.