creating instances

G

Guest

My requirement is to dynamically specify a class at runtime. I wrote the following code. it's nor functioning
can anyonme help em out

namespace ConsoleApplication

/// <summary
/// Summary description for Class1


/// </summary
///
class

public int a=90
string s="l;k;"


class Class


static void Main(string[] args

ObjectHandle obj = Activator.CreateInstanceFrom("ConsoleApplication1","A"); .// error: Filenotfound ecxeption assembly name is not the apt on

Console.WriteLine("jdksjf")
Console.WriteLine()
A a =(A)obj.Unwrap()
Console.Write(a.a)

}
 
J

Jon Skeet [C# MVP]

reiks said:
My requirement is to dynamically specify a class at runtime. I wrote
the following code. it's nor functioning. can anyonme help em out.

In your call to Activator.CreateInstanceFrom, you've specified the
*namespace* and then type name, rather than assembly and then *full*
type name.

However, you don't need to use that version anyway. It's simpler to
use:

Activator.CreateInstance("ConsoleApplication1.A");

Note that if the type were in a different assembly, you'd use something
like:

Activator.CreateInstance("MyAssembly.dll", "ConsoleApplication1.A");
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
Note that if the type were in a different assembly, you'd use something
like:

Activator.CreateInstance("MyAssembly.dll", "ConsoleApplication1.A");

Oops - although you could, you might be better to load the assembly
separately, then use loadedAssembly.CreateInstance(typeName) instead,
to save the object unwrapping. (Frankly, I still don't entirely
understand ObjectHandles etc...)
 

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