Use typeof with a variable

270 Views Asked by At

frmMainPage is a Form in my project.

This is correct

var myType = typeof(frmMainPage);

This is incorrect

Form frm = (Form)Activator.CreateInstance(Type.GetType("frmMainPage"), _userName);

var myType = typeof(frm);

How can I use typeof with a variable?

1

There are 1 best solutions below

0
Frenchy On

typeof takes a type name you specify at compile time.

GetType gets the runtime type of an instance.