c# execute a string as code

后端 未结 6 1718
难免孤独
难免孤独 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:07

    Executing a string as if it were code is possible in c#, but it's not pretty or simple. It's also considered poor practice and insecure (you probably should avoid it in dynamic languages, too).

    Instead, do something like this:

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

    Now, in your case you want to call a web service. Since that ultimately comes down to xml anyway you have a couple options:

    • Bypass the built-in system for calling web services and create your own web request with the correct name in the correct place in the xml.
    • Create delegates for each of the methods in the service to pass around, possibly via reflection.

提交回复
热议问题