Store a class in a variable

1

I would like to store a Class in a variable, in order to use that variable whether it is the class that is being stored. I had thought of a variable of type object

if (nameForm == "Cliente")
        {
            Cliente _cliente = new Cliente();
            var sourceType = _cliente.GetType();
            var tipoEntidad = sourceType.GenericTypeArguments[0];
            PropertyInfo[] property = tipoEntidad.GetProperties();

I do not need to instantiate the class, I need the class itself. How can I store a class in a variable?

    
asked by Pedro Ávila 15.10.2016 в 21:44
source

1 answer

1

Declare your variable as System.Type , which is the basis of the reflection .

like this:

Type unaClase;
//obtener la clase de una variable
unaClase = myVariable.GetType();
Type otraClase;
//asignar directamente una clase
otraClase = typeof(MiClase);

If you had to instantiate the class, you can use Activator.CreateInstance

UserControl tmp = (UserControl) Activator.CreateInstance(unaClase);

[edit]: I have edited the answer to add an example of direct assignment.

    
answered by 15.10.2016 / 22:05
source