Convert string to function

Asked

Viewed 67 times

0

What is the best way to make a string to void conversion uncomplicated.

I tried to use this code but it appears NullException

        private void Mainform_Load(object sender, EventArgs e)
        {   var method = "public void MSG(object o){ MessageBox.Show(o); }";
            method.GetType().GetMethod("MSG").Invoke("MSG", new object[] { "ZZZ" }); //Exception Aqui
        }

But it returns the following:

NullException
In Place (VS2012) appears the following: Locals

My case is as follows. I want to develop a game Auncher, but this Auncher should be made based on actions SortedDictionary<int, Acion> where int is the action id and Action is the action based on a string method that is converted to it.

And in an XML would be all the detailed actions:

<?xml version="1.0" encoding="utf-8" ?> 
<!-- DO NOT EDIT THIS FILE OR ALL OF THE FUNCTIONS EVEN BECOME INVALID AND UNSTABLE. IN THE CASE MAY RESULT IN SYSTEM FAILURE, AMONG ALL OTHER ERRORS IN COMMON. -->
<rels>
  <rel id="0" exec="play_button" />
  <rel id="1" exec="reg_button" />

  <rel id="2" exec="home_button" />
  <rel id="3" exec="clan_button" />
  <rel id="4" exec="shop_button" />
  <rel id="5" exec="forum_button" />
</rels>

Where rel is declaring the new action, id is the id in int and exec tells Sorteddictionary who should run the function.

And when he prepares to perform the action:

public async void executar_acao(int id){await Task.Run(lista_de_acoes[id]);
  • The best way is not to. In general people try to do this for the wrong reasons. If you really need to do this, then you’d better set up a script. Which is a lot easier now with the new .Net Compiler Platform.

1 answer

5


This is the incredibly wrong way to do what you want. You want to define a function at runtime, which is interesting, but not by String that you must do this.

The right thing to do is to use delegates. The use is like this:

using System;

public class Program
{
    public delegate void MeuTipoDeDelegate(object o);

    public static void Main()
    {
        MeuTipoDeDelegate teste = delegate(object o) {
            Console.WriteLine(o);
        };

        String umaStringQualquer = "Oi, eu sou uma String";
        teste(umaStringQualquer);
    }
}

I made you a Fiddle to demonstrate this. As in your case you want to define a dictionary of actions, you can do the following:

public delegate void MinhaAcao(object o);
public SortedDictionary<int, MinhaAcao> dicionarioTeste;

Then you define somewhere in your code the delegate of every action:

MinhaAcao acaoDePlay = delegate(object o) 
{
    /* Escreva aqui a função normalmente */
};

dicionarioTeste.Add(0, acaoDePlay);
  • Yes. worked, but now how do I load this dll application automatically on startup?

  • why my acadeDePlay should be in an external library.

  • will work as if it were "plugins"

  • 1

    Yes, the idea is the same. You can define your delegates in plugins and just call them in the main application.

  • To load the DLL at runtime, I’ll ask you another question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.