Create class instance from string

2020-03-25 02:59发布

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.

7条回答
别忘想泡老子
2楼-- · 2020-03-25 03:46

You probably don't have a type of "classPrefix_" plus whatever you have on className. The Type.GetType() call returns null and CreateInstance throws the ArgumentNullException.

查看更多
登录 后发表回答