从C#中的字符串执行函数 [英] Executing a Function from a String in C#

查看:90
本文介绍了从C#中的字符串执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含函数的字符串:



  public   string  func =  @  public int Add(int x,int y)
{
int z;
if((x == 10)||(y == 20))
{
z = x + y;
}
其他
{
z = x;
}

返回z;
}
;





我想在我的主要方法中调用它来执行:



 静态  void  Main( string  [] args)
{
类型type1 = typeof (Class1 );
// 实例
object obj = Activator.CreateInstance(type1);
object [] mParam = new object [] { 5 10 };
// 调用方法,将mParam传递给选定的代码
int res =( int )type1.InvokeMember( < span class =code-string> func,BindingFlags.InvokeMethod, null ,obj,mParam);
Console.Write( 结果:{0} \ n,res);
Console.ReadKey();
}





整个代码如下:



  class  Class1 
{
public < span class =code-keyword> string func = @ public int Add(int x,int y)
{
int z;
if((x == 10)||(y == 20))
{
z = x + y;
}
其他
{
z = x;
}

返回z;
}
;

静态 void Main( string [] args)
{
类型type1 = typeof (Class1);
// 实例
object obj = Activator.CreateInstance(type1);
object [] mParam = new object [] { 5 10 };
// 调用方法,将mParam传递给选定的代码
int res =( int )type1.InvokeMember( < span class =code-string> Multiply,BindingFlags.InvokeMethod, null ,obj,mParam);
Console.Write( 结果:{0} \ n,res);
Console.ReadKey();
}
}





所以我想知道是否有办法从我的字符串函数执行该函数在我的主要方法中。

谢谢。



如果我在字符串中传递该函数中的某些值?我将如何做到这一点,我将如何执行?很抱歉,如果我有很多问题。

解决方案

不,不仅仅是一个功能。这不是很明显吗?它没有意义,因为这样的函数的上下文是未知的。



如果为整个程序集和引用的程序集提供有效的源代码,你可以做这些事情。这是通过CodeDOM完成的:

http://msdn.microsoft.com/ en-us / library / hh156524.aspx [ ^ ],

http://msdn.microsoft。 com / en-us / library / f1dfsbhc.aspx [ ^ ]。



请查看我过去的答案:

使用CodeDom生成代码 [ ^ ],

创建使用Reloa的WPF应用程序dable插件...... [ ^ ]。







请不要重发。如果您还有其他问题,可以使用改善问题(见上文)对现有解决方案进行评论或添加说明。



-SA

Hi, I have this string that holds a function:

public string func = @"public int Add(int x, int y)
        {
            int z;
            if ((x == 10) || (y == 20))
            {
                z = x + y;
            }
            else
            {
                z = x;
            }

            return z;
        }";



And I want to call it in my main method to be executed:

static void Main(string[] args)
        {
            Type type1 = typeof(Class1);
            //Instance
            object obj = Activator.CreateInstance(type1);
            object[] mParam = new object[] { 5, 10 };
            //invoke method, pass mParam to selected code
            int res = (int)type1.InvokeMember("func", BindingFlags.InvokeMethod, null, obj, mParam);
            Console.Write("Result: {0} \n", res);
            Console.ReadKey();
        }



The whole Code looks like this:

class Class1
    {
        public string func = @"public int Add(int x, int y)
        {
            int z;
            if ((x == 10) || (y == 20))
            {
                z = x + y;
            }
            else
            {
                z = x;
            }

            return z;
        }";

        static void Main(string[] args)
        {
            Type type1 = typeof(Class1);
            //Instance
            object obj = Activator.CreateInstance(type1);
            object[] mParam = new object[] { 5, 10 };
            //invoke method, pass mParam to selected code
            int res = (int)type1.InvokeMember("Multiply", BindingFlags.InvokeMethod, null, obj, mParam);
            Console.Write("Result: {0} \n", res);
            Console.ReadKey();
        }
    }



So I was wondering if there is a way to execute the function from my string func in my main method.
Thanks.

If I will pass some values in that function inside a string? how will I do that and how will I execute that? Sorry if i have a lot of questions.

解决方案

No, not just a function. Isn''t it obvious? It would not make sense because the context of such function would be unknown.

You can do such things if provide valid source code for whole assembly and referenced assemblies if applicable. This is done via CodeDOM:
http://msdn.microsoft.com/en-us/library/hh156524.aspx[^],
http://msdn.microsoft.com/en-us/library/f1dfsbhc.aspx[^].

Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

[EDIT]

And please don''t re-post. If you have further questions, comment existing solutions or add clarifications to your questions using "Improve question" (see above).

—SA


这篇关于从C#中的字符串执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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