Creating object from type

  • Thread starter Thread starter Irfan
  • Start date Start date
I

Irfan

Hi all
I am new to DotNet and C#

Is there any way to get a type of object from a string. Lets suppose i have
a string variable containing a type "MyNameSpace.MyComponent" where
MyComponent.dll is already been referenced in the client project.

Now i want to create any object from that string. I think this require
getting a type from that string and then creating object from that type. If
this or any alternative is possible, pls let me know with a little example.

Regards,
Irfan
 
Hi,

Yes. You can.

If you want to manipulate the dynamically created objects, then you need to
have reference to them.

To do that either you can go for Interfaces and Classes.



eg)

using System;

using System.Reflection;

namespace sampleNamespace

{

public class Class1

{

public Class1()

{

Assembly sampleAssembly = Assembly.LoadFrom(@"c:\\sample.dll");

ISample sampleObj =
sampleAssembly.CreateInstance("sampleNameSpace.SampleClass");

sampleObj.sampleMethod(1);

}

}

}





Regards,

R.Balaji
 

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

Back
Top