Calling overloaded constructors using Reflection

R

R.Balaji

Hi,

How do I call the overloaded construction when I create the instance using
the Reflection?

for eg)

namespace MySpace
{
Interface IOrganization
{
int show();
}
}


namespace MySpace
{
class Organization : IOrganization
{
public Organization()
{
.....
}
public Organization(int orgNo)
{
.....
}
public int show()
{
.....
}
}
}

I create the instance using the reflection like this.
Assembly orgAssembly = Assembly.LoadFrom(@"c:\org.dll");

IOrganization m_organization =
(IOrganization)orgAssembly.CreateInstance("MySpace.Organization");



When I want to create an instance of the class Organization using
refelection, always it calls the default constructor.
How do I call the overloaded constructor? How do I pass the parameters?


Thanks.

Regards,
R.Balaji
 
J

Jon Skeet [C# MVP]

R.Balaji said:
How do I call the overloaded construction when I create the instance using
the Reflection?

Use Type.GetConstructor(Type[]) to get the appropriate constructor, and
then invoke it with the appropriate parameters.

Alternatively, use the form of Activator.CreateInstance which takes an
array of parameters, and hope they won't match more than one
constructor...
 

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