Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activator.CreateInstance returning error now

Tags:

c#

I was initially using the following method - which was working fine

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

Now I am getting an error at the above line and the error is:

Exception has been thrown by the target of an invocation.

Any suggestions on what might be going wrong ?

like image 603
Casper_2211 Avatar asked Feb 17 '23 15:02

Casper_2211


1 Answers

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

One tricky problem you might be having is if the exception is actually not thrown directly by the constructor, but by some field initializer.

For example the following would cause the behavior you described, even though there is no explicit constructor that could throw anything.

public class MyImpl
{
    private int something = ThisMethodThrows();

    private int ThisMethodThrows()
    {
        throw new Exception();
    }
}
like image 200
Ran Avatar answered Feb 26 '23 22:02

Ran