creating instances

  • Thread starter Thread starter Guest
  • Start date Start date
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)

}
 
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");
 
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...)
 
Back
Top