Activator.CreateInstance Error

B

bahadirarslan

Hi All,

I have a problem with Activator.CreateInstance method.
I have got a class like this

public class YaziYaz
{
public YaziYaz()
{
HttpContext.Current.Response.Write("Running");
}

public YaziYaz(object[] Params)
{
if (Params.Length == 0)
HttpContext.Current.Response.Write("No data");
else
{
foreach(object o in Params)
{
HttpContext.Current.Response.Write(o.ToString() +" <br /> ");
}
}
}
}

}

and i want to load this class dynamically with these codes

Assembly ass = Assembly.GetExecutingAssembly();
Type typ = ass.GetType("MyNameSpace.YaziYaz",false);
object ornek = Activator.CreateInstance(typ, Params); // error line

But i got an error:

Constructor on type MyNameSpace.YaziYaz not found.

Any suggestions?
 
B

bahadirarslan

I forgot my first message,
If i try like this

Assembly ass = Assembly.GetExecutingAssembly();
Type typ = ass.GetType("MyNameSpace.YaziYaz",false);
object ornek = Activator.CreateInstance(typ);

there is no error and "Running" is appearing on the screen
 
M

Marc Gravell

That is because your code says that "Params" is the array of parameters to
the method; however, this isn't what you want: you want to pass 1 parameter
that happens to be an array; to do this you need to wrap it in another
array:

Activator.CreateInstance(typ, new object[] { Params });

Just a gotcha in passing a single object[] to a method that uses "params"
arguments... (I mean the "params" argument to CreateInstance, not your code)

Marc
 
B

bahadirarslan

@Marc Gravell,

What can i say your, for example VERY THANKS :)

Thank you again. I solved :)
 

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