CreateInstance Contructor Parameters??

  • Thread starter Thread starter Bud via DotNetMonster.com
  • Start date Start date
B

Bud via DotNetMonster.com

How do you pass parameters to the contructor using create instance


Type t = Type.GetType("MyApp.Class1");
object obj = Activator.CreateInstance(t);

now all contructors for Class1 take at least one parameter

public class Class1
{
public Class1(Employee emp)
{

}

}

so how do I pass it emp
 
Hello,

Use overloaded method of CreateInstance to pass arguments to the
constructor.

Activator.CreateInstance(typeof(MyApp.Class1),new object[]{emp});

HTH, Cheers :)

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
thanks!!! worked like a champ.

One question. why do i need to instantiate a new object emp already exists.
 
Hello,
new object[]{emp} doesn't instantiate the Employee object. It
initializes an object array to pass as arguments to the constructor.

Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Back
Top