Exception has been thrown by the target of an invocation

B

Brett Kelly

Hello,

I'm trying to dynamically create instances of objects using
Activator.CreateInstance(), but I'm getting the above error.

Here's the code that calls CreateInstance:

-------------------------------
Type t = em.GetTypeByExt(ext);
object[] targs = new object[]
{me.Filename,me.TargetDirectory,me.ExecArgs};
BaseFile f = (BaseFile)System.Activator.CreateInstance(t,targs);
-------------------------------

Now, the Type t is correct (verified it with the visual studio
debugger), and targs are the constructor arguments.

Here's the constructor for BaseFile (an abstract class):

-------------------------------
public BaseFile(string fname, string tdir, string execargs)
{
this.Filename = fname;
this.TargetDirectory = tdir;
this.ExecArgs = execargs;
}
-------------------------------

I'm not sure what I'm doing wrong here, anybody have any suggestions?

Thanks!
Brett
 
T

Truong Hong Thi

Hi Brett,

What is the name of type t, does it a subclass of BaseFile?
You should post the constructor of t also.
Also, because BaseFile is abstract, it is good practice to mark its
constructor as protected.

Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top