Activator.CreateInstance ()

I used Activator.CreateInstance () in some of my codes. Is there a risk of making an instance using this?

+4
source share
1 answer

Well, there is a risk that your code will be poorly typed, and you wonโ€™t know that you accidentally tried to use it with a type that does not have an open constructor without parameters until runtime ... and it will be slightly worse than calling the constructor directly . Other than that, everything should be in order.

If you can design around it to use strongly typed factories, that would be preferable in many ways, but I fully understand that this is not always appropriate. In principle, this should be a bit of a final trick for those cases where normal design patterns fail, but this is a perfectly reasonable last resource :)

Do you have any specific problems?

+12
source

All Articles