Calling variable object from a string

Asked

Viewed 237 times

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 do not have a fixed object type

I found a similar question, but she won’t 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 tried the following code, but the types always return null.

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?

1 answer

2


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

Ex:

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

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

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

Browser other questions tagged

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