Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create class instance from string

I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code.

obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className));

ArgumentNullException was unhandled

Value cannot be null

Parameter name: type

Any help on this error would be appreciated.

like image 275
Tim Cooper Avatar asked Jan 22 '23 13:01

Tim Cooper


1 Answers

You may need to use the assembly qualified name as the argument to Type.GetType

eg AssemblyName.Namespace.ClassName

MSDN Doc on assembly qualified names

like image 185
Amal Sirisena Avatar answered Jan 25 '23 23:01

Amal Sirisena