You can use

        Activator.CreateInstance(Type)

Or one of the other overloads if you need to pass in constructor
parameters.

Greg Reinacker
Reinacker & Associates, Inc.
http://www.rassoc.com


-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Henrik Enemark Rasmussen
Sent: Tuesday, May 07, 2002 7:21 AM
To: [EMAIL PROTECTED]
Subject: HOWTO: Create object from type?


Hello all,

In C#, how can I create an object whoose type is not known until
run-time?
I need something similar to MFC's "RUNTIME_CLASS(CXxx).CreateObject()".

I expect it to look something like this, where the method will then
create an instance of the class and work with it.

public void ShowDialog(Type dialogClass)
{
   if (!dialogClass.IsSubclassOf(typeof(Dialog)))
      throw new ApplicationException("Invalid type");
   .
   .
   .
}

Regards
Henrik

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to