Use string to reference variable

1

I want to pass to the function execSQL the name of the variable that I want to use in % with% and from string SQL, pass the variable directly to the base.consulta() function, ie pass the variable that has the same name as string .

An example to give you an idea of what I'm looking for.

OBS: I know the way it is not working and I know why.

// variáveis com algum SQL
string sqlConsultaEspecifica = "...";
string sqlMonitoramento = "...";
string sqlVerificacao = "...";

function execSQL(string sql = "sqlMonitoramento") {
  // ...
  // realizar algumas ações aqui
  // ...
  return base.consulta(sql);
}

Is it possible to do this? How could I do that?

    
asked by anonymous 31.07.2018 / 18:18

3 answers

1

First of all, this syntax is not C #.

It is always possible to build a sophisticated mechanism that will allow this. But for what?

One of the ways is to use reflection, however almost 100% of cases that the person uses reflection is doing something wrong. It's a tremendous complication, a huge loss of performance, failure chance, insecurities for a gain that is often small or zero. I can almost guarantee you that this case is zero.

Where is the call of this method execSQL() ? This is where you will define what will happen. If the method already knows what it wants, then it does not make sense to use this.

If these variables are local to a method, it makes zero sense in trying to do something like this. If the object's variables are very simple:

void ExecSQL() {
    ...
    return base.consulta(sqlMonitoramento);
}

If the local variable is the reflection does not work. If class variable or instance reflection is unnecessary, zero gain.

If you want to do things differently, go to string different.

void ExecSQL(string sql) {
    ...
    return base.consulta(sql);
}

Then to call you pass, for example:

ExecSQL(sqlMonitoramento);

This code is very strange and may have an architectural flaw there. I can not say because it has no context.

    
31.07.2018 / 18:29
0

To get the value of a variable of some object it is possible to do the following:

Imagine an object named "Test" with 2 properties (Description and Name).

Let's say I want to get the value of either of these two properties, but I still do not know which one, so follow an example code with one of the ways to resolve this.

Teste teste = new Teste()
        {
            Descricao = "Descrição aqui",
            Nome = "nome aqui"
        };

        string valorDesejado = teste.GetType().GetProperty("Descricao").GetValue(teste, null).ToString();

Inside the "GetProperty ()" is the name of the variable that I want to get the value. So just adapt.

    
31.07.2018 / 18:28
0

You can try as follows:

public void execSQL(string sql)
{
    //A variável "valor" vai estar com o valor da variável que você passou como parâmetro.
    //O objeto "classe" deve ser o objeto que possui a sua variável "sql"
    string valor = teste.GetType().GetProperty(sql).GetValue(classe, null).ToString()   
}

The call to the method would look like this:

//Aqui você iria passar a variável que gostaria de passar o nome para o método, porém ela deve ser visível para esse escopo
execSQL(nameof(sqlMonitoramento));
    
31.07.2018 / 18:34