Call variable object from a string

0

I'm trying to call an object from a string, which contains its namespace, class and object name, however, all of these are variables and I do not have a fixed object type

I found a similar question , but she does not answer me.

In the above question, the code creates a list of a specific object, and I need to call objects from different classes.

I've tried the following code, but the types always return nulls.

class tstProcessa
{
    public static void processar(JSON.Arquivo Arq)
    {
        var DE = $"Origem.{Arq.Origem.Empresa}.{Arq.Origem.Sistema}.{Arq.Origem.Tabela}";
        //Origem.x.FINANCEIRO.cadcli;

        var PARA = $"Destino.{Arq.Destino.Empresa}.{Arq.Destino.Sistema}.{Arq.Destino.Tabela}";
        //Destino.y.FINANCEIRO.cadastro_cli;

        Type t1 = Type.GetType("Destino.y.FATURAMENTO.cadastro_cli, AssemblyName");
        Type t2 = Type.GetType(DE);
        Type t3 = Type.GetType("tstProcessa");

        int a1 = 0;
    }
}

Is there any way to Make this call?

    
asked by anonymous 06.10.2017 / 22:05

1 answer

2

There is a way to instantiate objects from strings, using Activator.CreateInstance .

Ex:

var DE = $"Origem.{Arq.Origem.Empresa}.{Arq.Origem.Sistema}+{Arq.Origem.Tabela}"
Activator.CreateInstance(Type.GetType(DE));

Being the path of GetType in the format "Namespace". "Class" + "Object".

Take a look at the reference of this method in MSDN .

    
06.10.2017 / 22:20