c# execute a string as code

后端 未结 6 1730
难免孤独
难免孤独 2020-12-10 18:44

Here is what i want to do, and i know it is possible with perl, php, python and java, but i am working with c#

how can i do the following:

public voi         


        
6条回答
  •  清歌不尽
    2020-12-10 19:23

    It can be done using reflection. However, I believe you need an object reference to go with it.

    Example from here

    Type t = this.GetType();
    MethodInfo method = t.GetMethod("showMessage");
    method.Invoke(this, null);
    

    Alternatively, you could use an Action or some other delegate to pass a reference to the function you want to call.

    public void amethod(Action function)
    {
        function();
    }
    

提交回复
热议问题