Activator Activation Error. CreateInstance

At first I used the following method - which worked fine

var obj = Activator.CreateInstance("MyProject","MyProject.MyImpl"); 

Now I get the error message in the above line, and the error:

 Exception has been thrown by the target of an invocation. 

Any suggestions on what might be wrong?

+4
source share
2 answers

The easiest way is to set a breakpoint in the constructor of the MyImpl class and debug it.

One difficult problem that you may encounter is that the exception was not actually selected directly by the constructor, but by some field initializer.

For example, the following will lead to the behavior you described, even if there is no explicit constructor that could throw anything.

 public class MyImpl { private int something = ThisMethodThrows(); private int ThisMethodThrows() { throw new Exception(); } } 
+5
source

An exception is thrown in the constructor of the object. Disable exceptions in Visual Studio , and it should break when creating this constructor.

+3
source

All Articles