Activator.CreateInstance - How to create instances of classes that have parameterized constructors

后端 未结 5 1757
野性不改
野性不改 2020-12-24 12:21

I have read a few bits and bobs online about this topic but found none that work for me. What I am trying to do is create a class of a runtime Type.

I use Acti

5条回答
  •  清歌不尽
    2020-12-24 12:30

    Activator.CreateInstance also has a whole bunch of overloads, one you might want to check out is ( Type type, params object[] args ). Simply supply the required constructor arguments to the second parameter of this call.

    Make sure you handle exceptions here though, as it's easy to pass incorrect parameters in or for something to change in the type's constructors later on that breaks it..

提交回复
热议问题